Hi AnkitPal,
Refer below code.
Code
C#
protected void OnSubmit(object sender, EventArgs e)
{
foreach (GridViewRow row in gvDetails.Rows)
{
int orderId = Convert.ToInt32(txtOrderId.Text);
string contactName = txtName.Text;
string itemName = row.Cells[1].Text;
decimal price = Convert.ToDecimal(row.Cells[2].Text);
decimal tax = Convert.ToDecimal(row.Cells[3].Text);
decimal total = Convert.ToDecimal(row.Cells[4].Text);
Insert(orderId, contactName, itemName, price, tax, total);
}
}
private void Insert(int orderId, string contactName, string itemName, decimal price, decimal tax, decimal total)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO table VALUES(@OId,@CName,@IName,@Price,@Tax,@Total)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@OId", orderId);
cmd.Parameters.AddWithValue("@CName", contactName);
cmd.Parameters.AddWithValue("@IName", itemName);
cmd.Parameters.AddWithValue("@Price", price);
cmd.Parameters.AddWithValue("@Tax", tax);
cmd.Parameters.AddWithValue("@Total", total);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvDetails.Rows
Dim orderId As Integer = Convert.ToInt32(txtOrderId.Text)
Dim contactName As String = txtName.Text
Dim itemName As String = row.Cells(1).Text
Dim price As Decimal = Convert.ToDecimal(row.Cells(2).Text)
Dim tax As Decimal = Convert.ToDecimal(row.Cells(3).Text)
Dim total As Decimal = Convert.ToDecimal(row.Cells(4).Text)
Insert(orderId, contactName, itemName, price, tax, total)
Next
End Sub
Private Sub Insert(ByVal orderId As Integer, ByVal contactName As String, ByVal itemName As String, ByVal price As Decimal, ByVal tax As Decimal, ByVal total As Decimal)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO table VALUES(@OId,@CName,@IName,@Price,@Tax,@Total)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@OId", orderId)
cmd.Parameters.AddWithValue("@CName", contactName)
cmd.Parameters.AddWithValue("@IName", itemName)
cmd.Parameters.AddWithValue("@Price", price)
cmd.Parameters.AddWithValue("@Tax", tax)
cmd.Parameters.AddWithValue("@Total", total)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub