6
Answers

Insert data from datagrid to Databse

Ask a question
SAM J

SAM J

13y
2.3k
1

inserting one row at a time....
for (int count = 0; count < dataGridView1.RowCount; count++)
                {
                        // here i am auto generating the ID
                    scd = new SqlCommand("Select count(Followup_ID) from Client_Followup", cnn);
                    cnn.Open();
                    object result = scd.ExecuteScalar();
                    cnn.Close();
                    auto = ("CF" + (int.Parse(result.ToString()) + 1));
                       
                        //inserting my datagrid values to database

                    scd = new SqlCommand("insert into Client_Followup(Followup_ID, Vac_ID, Vac_Designa) values(@Followup_ID, @Vac_ID, @Vac_Designa)", cnn);
                    scd.Parameters.AddWithValue("@Followup_ID", SqlDbType.VarChar).Value = auto;

                    scd.Parameters.AddWithValue("@Vac_ID", SqlDbType.VarChar).Value = dataGridView1[0, count].Value.ToString();

                    scd.Parameters.AddWithValue("@Vac_Designa", SqlDbType.VarChar).Value = dataGridView1[1, count].Value.ToString();

cnn.Open();
                    scd.ExecuteNonQuery();
                    MessageBox.Show("Row " + count + " Inserted Successfully");
                    cnn.Close();

Is this the right way to insert the rows one at a time??

Answers (6)