Hello Guys, i am facing a problem and problem is that i insert data in database using datagridview but it's only insert one row data even there is multiple row in datagridview.
please help me please
Thanks
What I have tried:
if (dataGridView1.Rows.Count < 0)
{
MessageBox.Show("Data Cannot be Insert", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into [Sales Record] ([Product Name],[Price],[Quantity],[Customer Name],[Date],[Total Pro Price],[Bill Number],[Cash Sales],[Size])values('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "'," + dataGridView1.Rows[i].Cells[1].Value.ToString() + "," + dataGridView1.Rows[i].Cells[2].Value.ToString() + ",'" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "',#" + dataGridView1.Rows[i].Cells[4].Value.ToString() + "#," + dataGridView1.Rows[i].Cells[5].Value.ToString() + "," + dataGridView1.Rows[i].Cells[6].Value.ToString() + ",'" + "Cash Sales" + "','" + dataGridView1.Rows[i].Cells[6].Value.ToString() + "');";
command.ExecuteNonQuery();
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
ad.InsertCommand = new OleDbCommand("UPDATE [Product Management] set [Quantity] = " + dataGridView1.Rows[j].Cells[7].Value + " where [Product Name] = '" + dataGridView1.Rows[j].Cells[0].Value + "'", connection);
ad.InsertCommand.ExecuteNonQuery();
}
printreceiptToken1();
listBox2.Items.Clear();
listBox3.Items.Clear();
listBox4.Items.Clear();
listBox5.Items.Clear();
textBox2.Text = "";
textBox4.Text = "";
label1.Text = "";
textBox1.Text = "";
listBox1.SelectedIndex = 0;
MessageBox.Show("Data Inserted Successfully", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
dataGridView1.Rows.Clear();
connection.Close();
}
}