Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.42/5 (4 votes)
See more:
Hi
I am using C# GUI and i have one dataGridview and one button i want to save all the row and column to sql table from grid please help me.....
Posted
Updated 28-Sep-17 21:20pm

SQL
are you storing the datagridview row values in a dataset?
if so , you can pass this dataset to a stored procedure to insert into a database table

check this to know how to pass dataset to a stored procedure.
Passing a datatable to a Stored Procedure in SQL Server 2008[^]
 
Share this answer
 
Assuming your DataGridView is not bound to the DB (or it would already contain rows in the database which you don't want to insert again) you would be best off using a loop:
C#
using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand cmd = new SqlCommand("INSERT INTO MyTable(Column1, Column2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.Add(new SqlParameter("@C1", SqlDbType.VarChar));
        cmd.Parameters.Add(new SqlParameter("@C2", SqlDbType.VarChar));
        con.Open();
        foreach (DataGridViewRow row in myDataGridView.Rows)
            {
            if (!row.IsNewRow)
                {
                cmd.Parameters["@C1"].Value = row.Cells[0].Value;
                cmd.Parameters["@C2"].Value = row.Cells[1].Value;
                cmd.ExecuteNonQuery();
                }
            }
        }
    }
 
Share this answer
 
Comments
The Doer 19-Dec-13 23:34pm    
if (!row.IsNewRow) This will return OLD data, Not the newly inserted one.
The Doer 19-Dec-13 23:37pm    
(!row.IsNewRow) this will return OLD rows.
Member 12588718 30-Jun-16 5:52am    
Object reference not set to an instance of an object.
Hi,

Create an xml for the grid data and send the entire xml to the store procedure.

Inside the SP you can parse it and can insert records using one insert statement.

In this case you will call SP one time. You don't need to call SP multiple time.

Thanks :)
 
Share this answer
 
Your code works a 100% great, i modificate the code for use with stored procedure but it's the same look!

void insertardgv2()
    {
 
        SqlConnection con = 
        new SqlConnection("Server=mypc Database=AdventureWorks2012;Trusted_Connection=True;");

        SqlCommand cmd = new SqlCommand("insertardatos", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@C1", SqlDbType.NVarChar));
        cmd.Parameters.Add(new SqlParameter("@C2", SqlDbType.NVarChar));

        con.Open();

        foreach (DataGridViewRow row in dataGridView2.Rows)
            {
            if (!row.IsNewRow)
                {
                cmd.Parameters["@C1"].Value = row.Cells[0].Value;
                cmd.Parameters["@C2"].Value = row.Cells[1].Value;
                cmd.ExecuteNonQuery();
                }
            }
    }


and here are my stored procedure in sql

SQL
create procedure insertardatos
(
   @C1 nvarchar (40),
   @C2 nvarchar (40)
)

as

INSERT INTO Contacts(CompanyName, Phone) VALUES (@C1, @C2);

go
 
Share this answer
 
SqlConnection con = new SqlConnection(@"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;");
SqlDataAdapter da=new SqlDataAdapter("select * from empdata",con);
DataSet ds=new DataSet();
da.Fill(ds);
grddata.DataSource=ds;
grddata.DataBind();

it will work for 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