在我的程序中,它具有用于插入、更新或删除采购订单的 datagridview,它的行为类似于购物车。这个 datagridview 由 BookName、ISBNNo、订单数量、单价和总计组成。当我将此 datagridview 数据插入数据库时,每一行插入具有标识值的唯一 id PO_Cart_No(pk)。
还有其他背景文本框详细信息,即 Po_No、supplier_Id 和总计详细信息插入到名为 PO 表的单独表中。这两个表也与外键 PO_No 链接。我的问题是当我向现有行添加新行以更新数据库时,该新行未插入,其他行数据已更新。问题出在哪里。这是我给你的代码,
public void UpdatePurchseOrderTable(int PO_No,int Supplier_ID, string Date, string RequiredDate, double GrandTotal)
{
DynamicConnection con = new DynamicConnection();
con.mysqlconnection();
string query = "UPDATE TBL_PO "
+ " SET Supplier_ID = @Supplier_ID,Date = @Date,"
+ "RequiredDate = @RequiredDate,GrandTotal=@GrandTotal"
+ " WHERE PO_No = @PO_No";
con.sqlquery(query);
con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
con.cmd.Parameters["@PO_No"].Value = PO_No;
con.cmd.Parameters.Add(new SqlParameter("@Supplier_ID", SqlDbType.Int));
con.cmd.Parameters["@Supplier_ID"].Value = Supplier_ID;
con.cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.Date));
con.cmd.Parameters["@Date"].Value = Date;
con.cmd.Parameters.Add(new SqlParameter("@RequiredDate", SqlDbType.Date));
con.cmd.Parameters["@RequiredDate"].Value = RequiredDate;
con.cmd.Parameters.Add(new SqlParameter("@GrandTotal", SqlDbType.Money));
con.cmd.Parameters["@GrandTotal"].Value = GrandTotal;
con.nonquery();
}
public void UpdatePOCartTable(int PO_No,string ISBN_No,int OrderQuantity, double UnitPrice, double Total)
{
DynamicConnection con = new DynamicConnection();
con.mysqlconnection();
string query = "UPDATE TBL_PO_Cart"
+ " SET ISBN_No = @ISBN_No, OrderQuantity= @OrderQuantity,"
+ "UnitPrice= @UnitPrice,Total=@Total"
+ " WHERE PO_No = @PO_No";
con.sqlquery(query);
con.cmd.Parameters.Add(new SqlParameter("@PO_No", SqlDbType.Int));
con.cmd.Parameters["@PO_No"].Value = PO_No;
con.cmd.Parameters.Add(new SqlParameter("@ISBN_No", SqlDbType.NVarChar));
con.cmd.Parameters["@ISBN_No"].Value = ISBN_No;
con.cmd.Parameters.Add(new SqlParameter("@OrderQuantity", SqlDbType.NVarChar));
con.cmd.Parameters["@OrderQuantity"].Value = OrderQuantity;
con.cmd.Parameters.Add(new SqlParameter("@UnitPrice", SqlDbType.Money));
con.cmd.Parameters["@UnitPrice"].Value = Math.Round(UnitPrice,2,MidpointRounding.AwayFromZero);
con.cmd.Parameters.Add(new SqlParameter("@Total", SqlDbType.Money));
con.cmd.Parameters["@Total"].Value = Math.Round(Total,2,MidpointRounding.AwayFromZero);
con.nonquery();
}
和PO表格数据如下
private void btnedit_Click(object sender, EventArgs e)
{
DynamicConnection con = new DynamicConnection();
try
{
if (txtPONo.Text != "" || cmbsupID.Text != "" || date1.Text != "" || requireddate.Text != "" || txtgrandTotal.Text != "")
{
PurchaseOrder PO = new PurchaseOrder();
if (cmbsupID.Text.Contains('-'))
{
string str = cmbsupID.Text;
int index = str.IndexOf('-');
if (index > 0)
{
int value = int.Parse(str.Substring(0, index));
PO.UpdatePurchseOrderTable(Convert.ToInt32(txtPONo.Text), value, date1.Text, requireddate.Text, Convert.ToDouble(txtgrandTotal.Text));
}
}
else
{
int value2 = Convert.ToInt32(cmbsupID.Text);
PO.UpdatePurchseOrderTable(Convert.ToInt32(txtPONo.Text), value2, date1.Text, requireddate.Text, Convert.ToDouble(txtgrandTotal.Text));
}
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
int PONO = Convert.ToInt32(txtPONo.Text);
string column1 = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
int column2 = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
double column3= Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
double column4 = Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
PO.UpdatePOCartTable(PONO,column1,column2,column3,column4);
}
}
else
{
MessageBox.Show("Please Provide Details!");
}
dataGridView1.Rows.Clear();
ClearData();
retviewPO_No();
MessageBox.Show("Record Updated Successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}