Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Get Data from Datagrid View and on button click i want to save the data into sql server database table.
It is an Windows Forms Application.
How i will get the Column Names dynamically from DataGridView and store the data into my table. Please Help me.
Posted
Comments
Thanks7872 30-Aug-13 5:27am    
From where the data comes into datagridview?
Jiban jyoti Rana 30-Aug-13 5:46am    
I have imported the data from an excel sheet.
Then, i want the data which is now imported from excel sheet to grid view will be saved to my table in database. Thanks in advance please help me....
Vedangi 30-Aug-13 6:28am    
no of columns fixed in datagridview ?
Jiban jyoti Rana 30-Aug-13 6:37am    
No, In datagrid view there are no columns. Whichever data i import from excel sheet that is only displayed in the datagrid view and they needs to go to database.

1 solution

This might work...but it's not the best programming practice

first create a storedprocedure that takes all field for your specific table but it's important that you declare them with a default values of null so that if you do not supply them you will not get an error...

create procedure spImportUsers
(
@Name nvarchar(20) = null
)

then in your button click event do something like this...

List<string> lstColumns = new List<string>();
            foreach (DataColumn dc in dataGridView1.Columns)
                lstColumns.Add(dc.ColumnName);
            foreach (DataRow row in dataGridView1.Rows)
            {
                using (SqlConnection conn = new SqlConnection(YourConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("spImportUsers", conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    for (int i = 0; i < lstColumns.Count; i++)
                        cmd.Parameters.AddWithValue(lstColumns[i].ToString(), row[lstColumns[i].ToString()].ToString());
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }


This solution will open a connection for each row but it will be easy to create a solution where you send through a list of values, I will leave that up to you...

Also take note that this solution is aimed at you knowing which table is being imported but not knowing which columns will be supplied, also that the columns being supplied are only column that are actually in the table.

When importing multiple values it is always important to create a transaction so that when one fails all the values will be rolled back... If you do not know how to create a transaction c# side or sql side if you google it you will get some helpful links

really hopes this helps you...
 
Share this answer
 

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