Kirron says:
private
void
btnClose_Click(
object
sender, EventArgs e)
{
for
(
int
i = 0; i < dataGridView1.Rows.Count; i++)
{
string
val =
this
.dataGridView1.Rows[i].Cells[0].Value
as
string
;
if
(
string
.IsNullOrEmpty(val) ==
false
)
{
con =
new
SqlConnection(constring);
con.Open();
cmd =
new
SqlCommand();
cmd.Connection = con;
cmd.CommandText =
"insert into Barcode(PartNumber, Description, Quantity, Bdate, Location, Mrp, ParentModel) values('"
+ dataGridView1.Rows[i].Cells[0].Value +
"', '"
+ dataGridView1.Rows[i].Cells[1].Value +
"', '"
+ dataGridView1.Rows[i].Cells[2].Value +
"', '"
+ dataGridView1.Rows[i].Cells[3].Value +
"', '"
+ dataGridView1.Rows[i].Cells[4].Value +
"', '"
+ dataGridView1.Rows[i].Cells[5].Value +
"', '"
+ dataGridView1.Rows[i].Cells[6].Value +
"')"
;
cmd.ExecuteNonQuery();
con.Close();
}
else
{
MessageBox.Show(
"Some of the contents of this excel files are empty!"
);
}
}
MessageBox.Show(
"Part Details Uploaded Successfully!"
);
}
Replace the above code with below.
private void btnClose_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
string val = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
if (!string.IsNullOrEmpty(val))
{
con = new SqlConnection(constring);
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into Barcode(PartNumber, Description, Quantity, Bdate, Location, Mrp, ParentModel) values('" + dataGridView1.Rows[i].Cells[0].Value + "', '" + dataGridView1.Rows[i].Cells[1].Value + "', '" + dataGridView1.Rows[i].Cells[2].Value + "', '" + dataGridView1.Rows[i].Cells[3].Value + "', '" + dataGridView1.Rows[i].Cells[4].Value + "', '" + dataGridView1.Rows[i].Cells[5].Value + "', '" + dataGridView1.Rows[i].Cells[6].Value + "')";
cmd.ExecuteNonQuery();
con.Close();
}
else
{
//this.dataGridView1.Rows[i].Cells[0].Visible = false;
MessageBox.Show("Some of the contents of this excel files are empty!");
}
}
MessageBox.Show("Part Details Uploaded Successfully!");
}