SAM J

SAM J

  • NA
  • 24
  • 33.2k

Insert data from datagrid to Databse

Mar 24 2011 2:54 PM

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)