How to save the data from DataGridView1 to SQL using this code?
I want to save my data to SQL using this code that contains for loop but I
found this problem "Input string was not in a correct format.".
private void btnDataSave_Click(object sender, EventArgs e)
{
using (SqlConnection CN = new SqlConnection(txtConString.Text))
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = CN;
command.CommandText = "insert into MRRDataStore(id, unit,
qty, desc, remark) values(@id, @unit, @qty, @desc,
@remark)";
CN.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
int id;
string unit;
string qty;
string desc;
string remark;
for (int i = 0; i <= dataGridView1.Rows.Count - 1;
i++)
{
id =
int.Parse(dataGridView1.Rows[i].Cells["Id"].Value.ToString());
unit =
dataGridView1.Rows[i].Cells["Unit"].Value.ToString();
qty =
dataGridView1.Rows[i].Cells["Qty"].Value.ToString();
desc =
dataGridView1.Rows[i].Cells["ItemDesc"].Value.ToString();
remark =
dataGridView1.Rows[i].Cells["Remarks"].Value.ToString();
}
}
}
}
}
}
No comments:
Post a Comment