hi,
how to assigning variables from gridview to database table in C#?
thanx
How to assigning variables from gridview to database table in C#?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Master BillaRank #2,229 · 630 reputationPosted Sep 14, 2009, 7:44 AM EDT
You can write code like this way.
private void SaveDataToDatabase()
{
using (SqlConnection con = new SqlConnection("connection"))
{
using (SqlCommand cmd = con.CreateCommand())
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
cmd.CommandText = "insert tableName(columns name here) values(" + item.Cells[0] + ")"; cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
}
Note:You can read cell data by index and set to query value.
thank you
Roei BarRank #1,271 · 1.4K reputationPosted Sep 14, 2009, 10:21 AM EDT
do you want to retrieve data by filter ?
can you provide some more specific information for me to help you out
roy himanshu98 reputationPosted Sep 14, 2009, 8:10 PM EDT