Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

Please help me ...

i have gridview having multiple rows and i want to save all data of the gridview into database at a time ,
ie., without looping

how can i do it?
Posted

Are you using the entity framework? This is possible if you use an EntityDataSource instead of a SqlDataSource. When you get the data in the code behind, you can just attach it to an entity context.
 
Share this answer
 
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value == null && row.Cells[1].Value == null && row.Cells[2].Value == null && row.Cells[3].Value == null && row.Cells[4].Value == null && row.Cells[5].Value == null && row.Cells[6].Value == null && row.Cells[7].Value == null)
{

}
else
{
con.Open();
OleDbCommand cmd = new OleDbCommand("insert into Sold(Product_name,Model_no,Seater,Leather,Colour,Unit_price,Quantity,Total,Advancepayment,Duespayment) values (@Product_name,@Model_no,@Seater,@Leather,@Colour,@Unit_price,@Quantity,@Total,@Advancepayment,@Duespayment)", con);

cmd.Parameters.AddWithValue("@Product_name", row.Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@Model_no", row.Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@Seater", row.Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@Leather", row.Cells[3].Value.ToString());
cmd.Parameters.AddWithValue("@Colour", row.Cells[4].Value.ToString());
cmd.Parameters.AddWithValue("@unit_price", row.Cells[5].Value.ToString());
cmd.Parameters.AddWithValue("@Quantity", row.Cells[6].Value.ToString());
cmd.Parameters.AddWithValue("@Total", row.Cells[7].Value.ToString());
cmd.Parameters.AddWithValue("@Advancepayment", textBox4.Text);
cmd.Parameters.AddWithValue("@Duespayment", label35.Text);

cmd.ExecuteNonQuery();
}
}

i don't know dear without looping so....
 
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