Click here to Skip to main content
15,038,237 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
George Jonsson 6-Jul-14 1:14am
   
What do you want me to do?
Write all the code for you from scratch?
Use the Improve Question button and add your current code so we can see what you have done so far.
It is not difficult to do this in one click. Just implement all necessary code in the Button.Click event.
Member 10532715 6-Jul-14 10:17am
   
sir I have just paste the coding.

Pls advise if any issue with my coidng.
George Jonsson 6-Jul-14 10:36am
   
What is not working with your code?

One thing you are missing is that you don't close your connection when done.
But that is a detail.
Member 10532715 6-Jul-14 10:38am
   
Its inserted but suppose in my excel 10 rows are available when i am going to insert in database its inserted one by one and getting message with ok after insert each row.
George Jonsson 6-Jul-14 10:45am
   
Do you mean you want to insert all 10 rows (or whatever the count) in one SQL statement?
What is the problem with the OK message?
Member 10532715 6-Jul-14 10:54am
   
i want to insert 10 rows one one click after that i need the massage "Data Export Sucessfully", "My Application"
Member 10532715 6-Jul-14 11:02am
   
sir pls help.
i cant do
Member 10532715 6-Jul-14 10:49am
   
i want to insert 10 rows one one click after that i need the massage "Data Export Sucessfully", "My Application"

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.
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);
   
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