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??
Loading

Guest UserRank #1,422 · 1.2K reputationPosted Mar 24, 2011, 11:18 AM EDT
SAM J18 reputationPosted Mar 24, 2011, 11:01 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Mar 24, 2011, 11:04 AM EDT
1. You're inserting the value for "auto" into column "Followup_ID". "auto" is a string but it looks like Followup_ID is an integer. That's going to cause an error when the insert executes.
2. If Followup_ID is an identity column, then you don't want to do inserts, you'll want to do updates where Followup_ID = auto.Substring(2) (to remove the CF). Otherwise you'll get an error because you'll be inserting duplicate values into Followup_ID.
SAM J18 reputationPosted Mar 24, 2011, 11:13 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Mar 24, 2011, 11:17 AM EDT
Validate the field length and the length of data you want to insert.
SAM J18 reputationPosted Mar 24, 2011, 11:27 AM EDT