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 UserPosted Mar 24, 2011, 3:18 PM
SAM JPosted Mar 24, 2011, 3:27 PM
Suthish NairPosted Mar 24, 2011, 3:17 PM
Validate the field length and the length of data you want to insert.
SAM JPosted Mar 24, 2011, 3:13 PM
Guest UserPosted Mar 24, 2011, 3:04 PM
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 JPosted Mar 24, 2011, 3:01 PM