andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi,
I got a Data Table which is generated at the Form_Load event. The data then populate in to a dataGridView.
The user is allowed to add values to a dataGridView column (issueQuantity) and when saving the data, I want to update two tables in the SQL server.
However, when I'm running the program, it will update only the first table but not the other.
Please find my code herewith.
Appreciate you feedback on how to accomplish this task please.
Kind regards,
Andrew
I got a Data Table which is generated at the Form_Load event. The data then populate in to a dataGridView.
The user is allowed to add values to a dataGridView column (issueQuantity) and when saving the data, I want to update two tables in the SQL server.
However, when I'm running the program, it will update only the first table but not the other.
Please find my code herewith.
C#:
SqlConnection connect = Connection.GetConnection();
table.AcceptChanges();
//Mark selected rows ready for insert
foreach (DataRow row in table.Rows)
{
if (row["Select Item"] as bool? == true)
{
row.SetAdded();
}
}
//Save Data to Destination
using (var command = new SqlCommand(@"UPDATE tbl_CostCentreDrugStock SET tbl_CostCentreDrugStock.availableQuantity += @aissueQuantity
FROM tbl_CostCentreDrugStock
INNER JOIN tbl_CostCentreOrderStatus
ON tbl_CostCentreDrugStock.costCentreID = tbl_CostCentreOrderStatus.costCentreID
WHERE tbl_CostCentreOrderStatus.ccOrderID =@accOrderID AND tbl_CostCentreDrugStock.drugID =@adrugID", connect))
using (var adapter = new SqlDataAdapter { InsertCommand = command })
{
command.Parameters.Add("@adrugID", SqlDbType.Int, 0, "drugID");
command.Parameters.Add("@accOrderID", SqlDbType.Int, 0, "ccOrderID");
command.Parameters.Add("@aissueQuantity", SqlDbType.Int, 0, "issueQuantity");
try
{
adapter.Update(table);
MessageBox.Show("Data Saved");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
command.Parameters.Clear();
}
using (var command = new SqlCommand(@"UPDATE tbl_DrugQuantity SET availableQuantity -= @aissueQuantity
WHERE drugID = @adrugID", connect))
using (var adapter = new SqlDataAdapter { InsertCommand = command })
{
command.Parameters.Add("@adrugID", SqlDbType.Int, 0, "drugID");
command.Parameters.Add("@aissueQuantity", SqlDbType.Int, 0, "issueQuantity");
try
{
adapter.Update(table);
MessageBox.Show("Data Saved");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
command.Parameters.Clear();
}
Appreciate you feedback on how to accomplish this task please.
Kind regards,
Andrew