使用多个字段值填充字段

本文关键字:字段 填充 | 更新日期: 2023-09-27 17:49:27

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

现在我所做的是,我已经使用了一个方法来尝试填充字段Contract Number。这个方法叫做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#的新手。

使用多个字段值填充字段

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

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

背景:
根据Value属性的文档,对于Element节点它是空的。你的sSequence节点很可能是一个Element节点。

关于你的方法popSeq:

    不要用throw ex重新抛出异常。有关更多信息,请参阅以下问题:什么可以导致throw重置调用堆栈(我'm使用&;throw&;,而不是&;throw&;)。
  • 不要捕捉异常,如果你不处理它们(重新抛出不算作处理!)