Use this code to clear the parameters before reassign new parameter value for each loop.
using (SqlCommand cmd = new SqlCommand("[Insert_DisplayDetail]", con))
{
foreach (GridViewRow row in Gridview1.Rows)
{
FileUpload upload = (FileUpload)row.FindControl("FileUpload1");
// if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(upload.PostedFile.FileName))
{
System.IO.Stream fs = upload.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@ID", _ID);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Or declare the SqlCommand inside the foreach loop.
foreach (GridViewRow row in Gridview1.Rows)
{
using (SqlCommand cmd = new SqlCommand("[Insert_DisplayDetail]", con))
{
FileUpload upload = (FileUpload)row.FindControl("FileUpload1");
// if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(upload.PostedFile.FileName))
{
System.IO.Stream fs = upload.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", _ID);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}