Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Memeber,

I need to Insert DataGridViewRow to database from excel. one click

Pls help

This my Code

C#
{
  SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AirtelMis"].ConnectionString);

  try
  {
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "insert into Airm1 values" + "(@SlNo, @name, @age,@amount,@Chqdate)";
    cmd.Parameters.Add("@SlNo", SqlDbType.VarChar);
    cmd.Parameters.Add("@Name", SqlDbType.VarChar);
    cmd.Parameters.Add("@age", SqlDbType.VarChar);
    cmd.Parameters.Add("@amount", SqlDbType.Decimal,2); 
    cmd.Parameters.Add("@Chqdate", SqlDbType.Date);
                

    for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
    {
      cmd.Parameters["@SlNo"].Value = this.dataGridView1[0,i].Value;
      cmd.Parameters["@name"].Value = this.dataGridView1[1,i].Value;
      cmd.Parameters["@age"].Value = this.dataGridView1[2,i ].Value;
      cmd.Parameters["@amount"].Value = this.dataGridView1[3, i].Value;
      cmd.Parameters["@Chqdate"].Value = this.dataGridView1[4, i].Value;
                        
      cmd.ExecuteNonQuery();
      MessageBox.Show("Data Export Sucessfully", "My Application",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
    this.Close();
  }
  catch (SqlException er)
  {
    MessageBox.Show("Error:" + er.ToString());
  }
}
Posted
Updated 6-Jul-14 4:31am
v3
Comments
George Jonsson 6-Jul-14 0:42am    
Do you want to get data from Excel into a DataGridView in c# and then insert into your database?
What have you tried so far?
Member 10532715 6-Jul-14 0:43am    
Yes Sir I need the same.
George Jonsson 6-Jul-14 0:45am    
So where are you stuck?
Member 10532715 6-Jul-14 0:49am    
I want to Insert All row at only one click. When I am trying to Insert it's inserted one by one and getting ok massage after insert.
Member 10532715 6-Jul-14 0:59am    
SIR help me

There are many examples, you can easily get by searching in Google. Below is one of them.
C# DataGridView Database Operations[^]

Try some code on your own and come back if you face any issues.
 
Share this answer
 
Just place the MessagBox outside the loop.

C#
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
  cmd.Parameters["@SlNo"].Value = this.dataGridView1[0,i].Value;
  cmd.Parameters["@name"].Value = this.dataGridView1[1,i].Value;
  cmd.Parameters["@age"].Value = this.dataGridView1[2,i ].Value;
  cmd.Parameters["@amount"].Value = this.dataGridView1[3, i].Value;
  cmd.Parameters["@Chqdate"].Value = this.dataGridView1[4, i].Value;

  cmd.ExecuteNonQuery();
}
MessageBox.Show("Data Export Sucessfully", "My Application",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
 
Share this answer
 
Comments
Member 10532715 6-Jul-14 11:18am    
Daar Sir,

Thanks a Lot its working fine..

realy you are the best
George Jonsson 6-Jul-14 11:38am    
You are welcome

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