Search code examples
c#xmlinfopathauto-populating

Populating a field using multiple field values


I'm using InfoPath 2007, and coding in C#. What I want to do is populate the field Contract Number, with multiple fields, being Contract Period End Date (dtTo, which has to be in the format YYYY/MM, which is a datepicker that was selected), Project Number (ddlPortfolio, a drop down list selected), Sequence Number (which is retrieved from the database, SQL Server 2005) and Fund Type (bFundType, an Option button selected).

Now what I have done is that I have used a method to try to populate the field Contract Number. The method is called Generate ContractNo, which goes:

public string GenerateContractNo()
    {
        string sSequence = "";

        //IPCode.popSeq(this.CreateNavigator(), this.NamespaceManager, DataSources, this.MainDataSource);


        //string seqNo = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:sSequence", NamespaceManager).InnerXml;



        string sPortfolio = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:ddlPortfolio", NamespaceManager).InnerXml;
        string sYear = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:dtTo", NamespaceManager).InnerXml;
        string sMonth = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:dtTo", NamespaceManager).InnerXml;
        //string sSeq = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:sSequence", NamespaceManager).InnerXml;
        string sFundType = this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:bFundType", NamespaceManager).InnerXml;

        //Convert.ToInt16(sSeq);

       // return sYear + "/" + sMonth + "/" + sPortfolio + "/" + sSeq + "/" + sFundType;

        sSequence = sYear + "/" + sMonth + "/" + sPortfolio + "/" + sFundType;

        this.CreateNavigator().SelectSingleNode("/my:myRoot/my:SectionHeading/my:sSequence", this.NamespaceManager).SetValue(sSequence); 

        //CourseDetails = dtSDate.ToLongDateString() + " - " + dtEDate.ToLongDateString() + " | " + sLocation + " | " + SDCategory;

        //CourseDetailsFields = sTitle + "|" + dtSDate.ToLongDateString() + "|" + dtEDate.ToLongDateString() + "|" + sLocation + "|" + SDCategory;

        //lstDetails.Add(CourseDetailsFields, CourseDetailsFields); 
        return null;

    }

I've tried to also use a class called PopSeq, to populate the Sequence Number field, with a sequence number from the Database, which calls a stored procedure called, uspGetSeqNo. The following is the class, as well as the stored procedure:

static public int popSeq(XPathNavigator xnMyForm, XmlNamespaceManager ns, DataSourceCollection ds, DataSource mds)
    {

        try
        {

            SqlConnection conn = new SqlConnection(IPCode.defaultConnectionString);
            SqlCommand command = new SqlCommand("[uspGetSeqNo]", conn);

            conn.Open();            
            command.CommandType = CommandType.StoredProcedure;

            //XPathNavigator domNav = mds.CreateNavigator();
            //string sVendor = domNav.SelectSingleNode("/my:myRoot/my:SectionHeading/my:ddlVendor", this.NamespaceManager).ToString();

            //command.Parameters.AddWithValue("@iSequence", s);

            SqlDataReader myReader = command.ExecuteReader();



            while (myReader.Read())
            {
                string iSequence = Convert.ToString(myReader.GetOrdinal("iSequence"));
               //return Convert.ToInt16(sSeq);                    
            }
        }
        catch (Exception ex)
        {
            throw ex;

        }
        return 0;
    }

Stored Procedure:

USE [TAU_PANEL]
GO
/****** Object:  StoredProcedure [dbo].[uspGetSeqNo]    Script Date: 04/06/2011      08:52:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER PROCEDURE [dbo].[uspGetSeqNo]

AS

SELECT     MAX(iSequence) + 1 AS seq
FROM         dbo.ResAllocations

So my problem is that, on the button save , it doesn't populate the Contract Number field. Sorry as I'm a novice to C#.


Solution

  • As far as I can see, you don't need the return value of GenerateContractNo, so make the method to have a return type of void. To set the value, try using this code instead:

    this.CreateNavigator().
        SelectSingleNode("/my:myRoot/my:SectionHeading/my:sSequence", 
            this.NamespaceManager).InnerText = sSequence; 
    

    Background:
    According to the documentation for the Value property, it is null for Element nodes. Your sSequence node most likely is an Element node.

    About your method popSeq: