从一个表添加并更新另一个表

时间:2015-10-01 05:05:59

标签: c# asp.net

它正在添加到库存表,但是当我点击它时,它不会更新另一个表中的字段。是否可以添加和更新2个表。

这是在列表视图按钮外单击添加按钮

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

这段代码可能是我的错误?

2 个答案:

答案 0 :(得分:1)

试试这个

        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.ExecuteNonQuery();

cmd = new SqlCommand();
cmd.Connection = con;



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

答案 1 :(得分:1)

我建议您使用存储过程,但如果您希望使用内联查询,请用此替换您的代码。

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.ExecuteNonQuery();
        SqlCommand cmd1 = new SqlCommand("UPDATE PurchaseOrder SET POStatus=@POStatus WHERE PONo=@PONo", con);

        cmd1.Parameters.AddWithValue("@POStatus", status);
        cmd1.Parameters.AddWithValue("@PONo", txtPONo.Text);

        cmd1.ExecuteNonQuery();
        con.Close();

    }
    Response.Redirect("Default.aspx");         
}