while i was searching in google i find a code to add the items from datagridview to database using c# <pre lang="c#">for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { string strCmd = @"Insert into Bills(itemname) values(" + dataGridView1.Rows[i].Cells[0].Value + ")"; SqlCommand Cmd = new SqlCommand(strCmd, Con); Cmd.ExecuteNonQuery(); }
using (SqlConnection connection = new SqlConnection("-YOUR CONNECTION STRING-")) using (SqlCommand command = new SqlCommand("INSERT INTO Bills (ItemName) VALUES (@ItemName)", connection)) { SqlParameter pItemName = command.Parameters.Add("@ItemName", SqlDbType.VarChar, 50); connection.Open(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { pItemName.Value = dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value; command.ExecuteNonQuery(); } }
connection.Open()
for
null
pItemName.Value = dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)