在我的程序中有datagridview用于插入,更新或删除Purchase Order,它的行为就像购物车一样。这个datagridview包含BookName,ISBNNo,订单数量,单价和总数。当我将这个datagridview数据插入数据库时,每行插入唯一ID为PO_Cart_No(pk)且具有标识值。
还有其他背景文本框详细信息,即Po_No,supplier_Id和Grand total details插入到单独的表中,称为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);
}
}