andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi All,
I am working on a small application which got a dataGridView and the first columns got a "CheckBox".
My requirement is, if the user has selected/checked the records of the dataGridView, the said records should save in to the database table when hit the "Save" button.
However, i have come across that, the foreach command and the if condition (to check whether the user has selected an item) do not work together as per my code.
Please find the respective code below;
My database table looks like below;
Please advise me how to correct the above said issue.
Thank you in advance.
Kind regards,
Andrew
I am working on a small application which got a dataGridView and the first columns got a "CheckBox".
My requirement is, if the user has selected/checked the records of the dataGridView, the said records should save in to the database table when hit the "Save" button.
However, i have come across that, the foreach command and the if condition (to check whether the user has selected an item) do not work together as per my code.
Please find the respective code below;
C#:
SqlConnection con = new SqlConnection(@"Data Source=55504-CGHS\\SQLEXPRESS;Initial Catalog=Test_CheckBox;Integrated Security=True");
SqlCommand cmd;
using (con)
{
foreach (DataGridView row in dataGridViewProducts.Rows)
{
if (Convert.ToBoolean(dataGridViewProducts.Rows[row].Cells["selectItem"].Value) == true)
{
cmd = new SqlCommand(@"INSERT INTO tbl_Product(productID,productName,quantity) VALUES (@aproductID,@aproductName,@aquantity)", con);
cmd.Parameters.AddWithValue("@aproductID", Convert.ToInt32(row.Cells["textProductID"].Value == DBNull.Value ? "0" : row.Cells["textProductID"].Value.ToString()));
cmd.Parameters.AddWithValue("@aproductName", row.Cells["textProductName"].Value == DBNull.Value ? "0" : row.Cells["textProductName"].Value.ToString());
cmd.Parameters.AddWithValue("@aquantity", Convert.ToInt32(row.Cells["textQuantity"].Value == DBNull.Value ? "0" : row.Cells["textQuantity"].Value.ToString()));
cmd.ExecuteNonQuery();
}
}
}
My database table looks like below;
Please advise me how to correct the above said issue.
Thank you in advance.
Kind regards,
Andrew