Concentrate on your following code
for (int i = 0; i < names.Count; i++)
{
sCommand.Parameters.AddWithValue
("@debtor_name", Names[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_person", ContactPeople[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_number", Phones[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_address", Addresses[i].ToString());
}
You are Looping through the names List and adding parameter to sCommnand object.
If you directly run
sCommand.ExecuteNonQuery();
, it will throw an exception as there are more parameters than actually specified int the Stored Procedure.
Also you are calling
RowsAffected = adpt.Update(dt);
From your code there is no relation of
sCommand
&
dt
object.
adpt.InsertCommand = sCommand;
is meaningless in this context beacuse
dt
has no extra rows added.
Solution 1:
you may call like this
for (int i = 0; i < names.Count; i++)
{
sCommand.Parameters.AddWithValue
("@debtor_name", Names[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_person", ContactPeople[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_number", Phones[i].ToString());
sCommand.Parameters.AddWithValue
("@contact_address", Addresses[i].ToString());
sCommand.ExecuteNonQuery();
sCommand.Parameters.Clear();
}
Solution 2:
for (int i = 0; i < names.Count; i++)
{
DataRow f = dt.NewRow();
f["@debtor_name"] = Names[i].ToString();
f["@contact_person"] = ContactPeople[i].ToString();
f["@contact_number"] = Phones[i].ToString();
f["@contact_address"] = Addresses[i].ToString();
dt.Rows.Add(f);
}
Size = Names.Count;
adpt.UpdateBatchSize = size;
objConnection.Open();
RowsAffected = adpt.Update(dt);
objConnection.Close();