2017-10-14 46 views
0

我有两列boatRegNumPayerret代码只插入最后一行。有任何想法吗?

boatRegNum是字符数据类型和Payerret是钱的数据类型。

我们正在使用一个GridView创建动态行。第一行是默认创建的。

在该第一行,用户对boatRegNumPayerret进入值。出于演示的目的,在第一排为boatRegNumB3098和价值Payerret为同一行530.000

然后用户点击“添加新行”按钮来添加一个额外的行。在新添加的行中,用户输入TY000代替boatRegNum90.00作为第二行。

我们到目前为止遇到的问题是,当这些值插入到数据库中,仅第二行的值插入到数据库中。第一行的值总是被忽略。

任何想法我做错了什么?

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

标记:

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

C#:

// 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(); 
    } 
} 

存储过程:

ALTER PROCEDURE [dbo].[sp_saveInfo] 
    @pid INT, 
    @eID INT, 
    @txYr INT, 
    @regno VARCHAR(50), 
    @txretval DECIMAL(18, 2), 
    @tID INT 
AS 
BEGIN 
    SET NOCOUNT ON; 

    BEGIN TRAN 

    IF EXISTS (SELECT * FROM Info WHERE pid = @pid) 
    BEGIN 
     UPDATE Info 
     SET EquipmentTypeID = @eID, 
      TaxYear = @txYr, 
      AirCraftRegNo = @regno, 
      TaxPyrRetdVal = @txretval 
     WHERE pid = @pid 
    END 
    ELSE 
    BEGIN 
     INSERT INTO [dbo].Info (pid, TaxYear, AirCraftRegNo, EquipmentTypeID, TaxPyrRetdVal, TaxpayerID) 
     VALUES (@pid, @txYr, @regno, @eID, @txretval, @tID) 
    END 

    COMMIT TRAN 
END 
+0

您在插入语句中使用标量变量。标量一次只能保存一个值,因此您一次只能插入一行数据。你获得最后一个值的原因是因为第一个值被第二个值推出,等等......最后一行没有任何东西可以消除,所以这就是你得到的。 –

+0

帮我先生,我会如何改变这种情况? 我怀疑你的意思是存储过程。 – Kenny

+0

我可以向你展示使用直接sql的语法,但是当涉及到XML和JSON时,我毫无用处......实际上,我得到了一个可扩展的XML,它被传递到一个proc并解析到一个表中。我没有写XML部分...让我看看我能找到什么。 –

回答

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. 

希望你能够填写剩下的空白。