Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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();
                }
            }
Posted
Comments
cvogt61457 9-Dec-16 21:41pm    
Which row do you see? First? Last?
Are you getting some type of error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900