Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to send data from datagridview to database with progress bar using c#.net
Posted

1 solution

Here i am simply giving the link for sending data to database.

C#
void Save()
        {
            try
            {
                if (dataGridView1.Rows.Count > 1)
                {
                    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                    {
                        string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
                        {
                            string insert = "INSERT INTO InvoiceItems(SNo,Quantity,Rate,Description,Total) VALUES (@SNo,@Quantity,@Rate,@Description,@Total)";
                            con.Open();
                            SqlCommand cmd = new SqlCommand(insert, con);
                            cmd.Parameters.AddWithValue("@SNo", col1.ToString());
                            cmd.Parameters.AddWithValue("Description", col2.ToString());
                            cmd.Parameters.AddWithValue("@Quantity", col3.ToString());
                            cmd.Parameters.AddWithValue("@Rate", col4.ToString());
                            cmd.Parameters.AddWithValue("Total", col5.ToString());
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

I have not attached the progress bar in it.
Thanks Hope i helped you.
 
Share this answer
 
v3

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