use foreach to traverse through the values of the gridview and insert them into the database one row by row.
here is an example for you
protected void btnCheckout_Click(object sender, EventArgs e)
{
con.Open();
foreach(GridViewRow rowno in GridView1.Rows )
{
if (rowno.RowType == DataControlRowType.DataRow)
{
try
{
string productidnow = rowno.Cells[0].Text.ToString();
int quantitynow = int.Parse(((TextBox)rowno.Cells[2].FindControl("txtQuantity")).Text);
decimal pricenow = Convert.ToDecimal(rowno.Cells[3].Text.ToString());
decimal totalnow = Convert.ToDecimal(rowno.Cells[4].Text);
SqlCommand cmd = new SqlCommand("Insert into orders (productid, quantity , price, total) values (@productid,@quantity,@price,@total) ",con);
cmd.Parameters.AddWithValue("@productid", productidnow );
cmd.Parameters.AddWithValue("@quantity", quantitynow );
cmd.Parameters.AddWithValue("@price", pricenow );
cmd.Parameters.AddWithValue("@total", totalnow );
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
break;
}
}
}
}
here shopping cart items are inside the gridview. Now i have used foreach loop to traverse and add data to database for each row inside the gridview. hope it helps. Feel free to ask if u dont understand any part of the code.