Click here to Skip to main content
16,012,173 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have some rows in datagridview.

I want to save them together in database...

Please help me to do that
Thanks in Advance
Posted
Comments
[no name] 29-Mar-14 0:31am    
Just treat each product as an individual item and just loop through each product that needs to be inserted and do it.
Dealing with multiple rows is pretty much exactly the same as dealing with individual rows of data. The only difference is that multiple rows are dealt with in a loop of some kind.

Here is sample code try this:


protected void savedatafromgv()
       {

           foreach (DataGridRows g1 in dg_AgentSFR.Rows)
            {

                SqlConnection con = new SqlConnection(strConnString);
                SqlCommand cmd = con.CreateCommand();
                cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER,FORWARDER REFERENCE,SHIPPING LINE,CONTAINER TYPE,CONTAINER SIZE,VALIDITY FROM,VALIDITY TO,BASIC RATE,PAF,CAF,PSS,TOTAL AMOUNT,REE DAYS,CREDIT DAYS,NIT DEPOSIT,COMPANYID,ISACTIVE) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "','" + g1.Cells[14].Text + "','" + g1.Cells[15].Text + "','" + g1.Cells[16].Text + "',1,'" + TXTCompanyID.Text + "')", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            Response.Write("Records inserted successfully");

       }


to avoid sql injection use "cmd.parameter.add .................."
 
Share this answer
 
v2
 
Share this answer
 
v2
 
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