代码仅插入最后一行。有任何想法吗?

时间:2017-10-14 04:03:31

标签: c# asp.net sql-server

我有两列boatRegNumPayerret

boatRegNum是字符数据类型,Payerret是money数据类型。

我们使用GridView创建动态行。默认情况下会创建第一行。

在第一行,用户输入boatRegNumPayerret的值。出于演示目的,第一行boatRegNum的值为B3098,同一行的Payerret值为530.000

然后用户单击“添加新行”按钮以添加其他行。在新添加的行中,用户输入TY000的{​​{1}}和第二行的boatRegNum

到目前为止,我们遇到的问题是,当这些值插入到数据库中时,只会将第二行的值插入到数据库中。始终忽略第一行的值。

任何想法我做错了什么?

以下是相关代码。在此先感谢您的帮助。

标记:

90.00

C#:

<%foreach (System.Data.DataRow row in dtAirInfoTable.Rows)
{%>
    <tr>
        <td>
            <span class="form-control" style="width: 493px; color: #0093B2; font-weight: bold;">
            <%=Convert.ToString(row.ItemArray[1].ToString())%></span>
        </td>
        <td align="left">
            <span class="form-control txtPayerret" style="width: 326px; color: #0093B2; font-weight: bold;">
            <%=Convert.ToString(row.ItemArray[2].ToString())%></span>
        </td>
    </tr>
<% } %>

存储过程:

// datatable CurrTable
private DataTable LoadTable1(bool createIfMissing)
{
    const string ViewStateKey = "CurrTable";

    DataTable dt = ViewState[ViewStateKey] as DataTable;

    if (createIfMissing)
    {
        if (dt == null)
        {
            dt = new DataTable();

            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("boatregNum", typeof(string)));
            dt.Columns.Add(new DataColumn("Payerret", typeof(string)));

            ViewState[ViewStateKey] = dt;
        }

        if (dt.Rows.Count == 0)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = 1;
            dr["boatregNum"] = string.Empty;
            dr["Payerret"] = string.Empty;
            dt.Rows.Add(dr);
        }
    }

    return dt;
}

private void SetInitialRow()
{
    myMultiView.ActiveViewIndex = 0;

    DataTable dt = LoadTable1(true);

    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

DataTable regtable = ViewState["CurrTable"] as DataTable;    

foreach (DataRow row in regtable.Rows)
{
    string txBoatRegNum = row.ItemArray[1].ToString();
    string txTaxPayerRet = row.ItemArray[2].ToString();

    //prevent money data types from breaking. If user does not enter value, then enter 0 in its place
    //This sub will store Marine and Vessel info to the database.
    if (txTaxPayerRet != null)
    {
        if (string.IsNullOrEmpty(txTaxPayerRet))
        {
            txTaxPayerRet = "0";
        }

        SqlCommand aircmd = new SqlCommand("sp_saveInfo", conn);
        aircmd.CommandType = CommandType.StoredProcedure;

        aircmd.Parameters.AddWithValue("@pid", accountnumber.Text);
        aircmd.Parameters.AddWithValue("@eID", Request["pageId"]);
        aircmd.Parameters.AddWithValue("@tID", tPayerID);
        aircmd.Parameters.AddWithValue("@txYr", txtTaxYr.Text);
        aircmd.Parameters.AddWithValue("@regno", txBoatRegNum);
        aircmd.Parameters.Add("@txretval", SqlDbType.Money).Value = decimal.Parse(txTaxPayerRet); //convert text back to money

        aircmd.ExecuteNonQuery();
    }
}

1 个答案:

答案 0 :(得分:0)

好的......发现它......请注意,除了将concertin varchar传递给xml之外,没有使用标量变量。开发人员只是将select中的xml直接解析到表中。

CREATE PROCEDURE dbo.StoredProcName
/*
* mm/ff/yyyy xx, TASK:INC00000.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*/
@sXML VARCHAR(MAX)
AS
BEGIN
   SET NOCOUNT ON;

   BEGIN TRY
      BEGIN TRANSACTION;

      -- Change the VARCHAR(MAX) to XML data type.
      DECLARE @XML XML = @sXML;

      -- create a new temp table...
      IF OBJECT_ID('tempdb..#XMLTABLE', 'U') IS NOT NULL 
         DROP TABLE #XMLTABLE;

        CREATE TABLE #XMLTABLE (
            ItemNumberOne INT NOT NULL,
            UserID INT NOT NULL,
            ItemNumber3 INT NOT NULL,
            Priority SMALLINT NOT NULL,
            ActionType VARCHAR (3) NOT NULL
            );
        -- insert the shreaded xml into the #temp table
        INSERT INTO #XMLTABLE (UserPayorForInvoiceReviewID,UserID,PayorID,Priority,ActionType)
        SELECT
            ISNULL(NULLIF(ItemList.Item.query('./ItemNumberOne')    .value('.','INT'), 0), -1),
            ISNULL(NULLIF(ItemList.Item.query('./UserID')           .value('.','INT'), 0), -1),
            ISNULL(NULLIF(ItemList.Item.query('./ItemNumber3')      .value('.','INT'), 0), -1),
            ISNULL(NULLIF(ItemList.Item.query('./Priority')         .value('.','SMALLINT'), 0), -1),
            ItemList.Item.query('./ActionType')                     .value('.','VARCHAR(3)') ActionType
        FROM 
            @XML.nodes('/ItemList/Item') AS ItemList(Item);

        -- There's more to the rest of the procedure below
        -- but the the xml data is loaded into the temp table.

希望你能够填补余下的空白。