Click here to Skip to main content
14,389,354 members
Rate this:
Please Sign up or sign in to vote.
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 22:20pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
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();
                }
            }
        }
    }
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 4

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 :)
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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

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

as

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

go
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100