从列表视图添加多个数据

时间:2015-09-30 08:38:57

标签: c# asp.net

我有一个列表视图,告诉已订购的数量和已交付的数量。我已完成添加第一行,但未添加后续行。我想一键添加多个记录。我在列表视图外面有一个添加按钮来完成这项工作

以下是表单的列表视图

<asp:ListView ID="lvPODetails" runat="server">
                  <ItemTemplate>
                           <tr>
                              <td>
                                 <asp:Label ID="lblProduct" runat="server" type="number" Text='<%# Eval("RefNo")%>' class="form-control" Visible ="false" />
                                 <%# Eval("ProductName") %>
                              </td>
                              <td>
                                  <%# Eval("Price", "{0: #,###.00}") %>
                              </td>
                              <td><asp:TextBox ID="txtOrdered" runat="server" type="number" Text='<%# Eval("DesiredQuantity") %>'  readonly="true" class="form-control" /></td>
                              <td><asp:TextBox ID="txtQuantity" runat="server" type="number" min="1" max="1000000" Text='' class="form-control" required /></td>
                              <td><%# Eval("POAmount", "{0: #,###.00}") %></td>
                              <td><%# Eval("POAmount", "{0: #,###.00}") %></td>                                
                          </tr>

                    </ItemTemplate>
                  <EmptyDataTemplate>
                        <tr>
                            <td colspan="4"><h2 class="text-center">No records found.</h2></td>
                        </tr>
                  </EmptyDataTemplate>
</asp:ListView>

aspx的.cs

protected void btnAdd_Click(object sender, EventArgs e)
{
    bool existingSupply = IsExisting();
    foreach (ListViewItem item in lvPODetails.Items)
    {
        TextBox quantity = (TextBox)item.FindControl("txtQuantity");
        Label ltr = (Label)item.FindControl("lblProduct");
        TextBox odr = (TextBox)item.FindControl("txtOrdered");

        string name = ltr.Text;
        int delivered = Convert.ToInt32(quantity.Text);
        int ordered = Convert.ToInt32(odr.Text);
        string status = String.Empty;


        if (delivered >= ordered)
        {
            status = "Complete";

        }




        if (delivered < ordered)
        {
            status = "Partially Completed";

        }

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        if (existingSupply)
        {
            cmd.CommandText = "UPDATE Inventory SET Quantity=quantity + @Quantity WHERE ProductID=@ProductID";

        }

        else
        {
            cmd.CommandText = "INSERT INTO Inventory VALUES (@ProductID, @ProdCatID, @SupplierName, " +
                "@Quantity, @CriticalLevel, @Price, @Status, @DateAdded, @DateModified)";

        }

    cmd.Parameters.AddWithValue("@ProductID", name);
    cmd.Parameters.AddWithValue("@ProdCatID", DBNull.Value);
    cmd.Parameters.AddWithValue("@SupplierName", txtSupplier.Text);
    cmd.Parameters.AddWithValue("@Quantity", delivered);
    cmd.Parameters.AddWithValue("@CriticalLevel", DBNull.Value);
    cmd.Parameters.AddWithValue("@Price", DBNull.Value);
    cmd.Parameters.AddWithValue("@Status", DBNull.Value);
    cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
    cmd.Parameters.AddWithValue("@DateModified", DateTime.Now);

    //cmd.CommandText = "UPDATE PurchaseOrder SET POStatus=@POStatus WHERE PONo=@PONo";
    //cmd.Parameters.AddWithValue("@POStatus", status);
    //cmd.Parameters.AddWithValue("@PONo", txtPONo.Text);

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Default.aspx");                     
    }
} 

我没有得到它,它没有显示错误,但它没有添加下一行。

0 个答案:

没有答案