使用多个字段值填充字段

时间:2011-04-06 08:51:21

标签: c# xml infopath auto-populating

我正在使用InfoPath 2007,并在C#中编码。我想要做的是填写合同编号字段,其中包含多个字段,合同期限结束日期 dtTo ,必须在格式 YYYY / MM ,这是一个选定的日期选择器),项目编号 ddlPortfolio ,选择了一个下拉列表),序列号(从数据库中检索,SQL Server 2005)和基金类型( bFundType ,选择了一个选项按钮)。

现在我所做的是我使用了一种方法来尝试填充字段合同号。该方法称为Generate ContractNo,其中包括:

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;

    }

我还尝试使用一个名为PopSeq的类来填充序列号字段,其中包含来自数据库的序列号,该序列号调用名为uspGetSeqNo的存储过程。以下是类,以及存储过程:

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;
    }

存储过程:

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

所以我的问题是,在按钮保存时,它不会填充合同编号字段。抱歉,因为我是C#的新手。

1 个答案:

答案 0 :(得分:0)

据我所知,您不需要GenerateContractNo的返回值,因此请使方法的返回类型为void。要设置该值,请尝试使用此代码:

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

背景:
根据{{​​3}}的文档,对于Element节点,它为null。您的sSequence节点很可能 是一个Element节点。

关于您的方法popSeq

  • 请勿使用throw ex重新抛出异常。有关详细信息,请参阅以下问题:Value property
  • 如果不处理异常,请不要捕捉异常(重新抛出不算作处理!)