Dear Friends,
I want to save Date from DataGridView to Database.
my Date Datatype is DateTime in DB.
please, check my code

private void button4_Click(object sender, EventArgs e)
{
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
SqlCommand cmd = new SqlCommand("INSERT INTO cat1_tab (myDate) VALUES(@myDate)", con);
cmd.Parameters.Add("@mydate",dataGridView1.Rows[i].Cells[7].Value.ToString("yyyy-MM-dd"));
cmd.ExecuteNonQuery();
MessageBox.Show("save");
con.Close();
}
}

I worry that such a code passage could drive the user mad.

Line 9 looks to have the user click OK for each row in the grid.
Line 10 looks to prematurely close the connection and send the app into an error condition.

Rewrite this to avoid both issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.