Hi akhter,
I have modified your code.
Code
C#
protected void btnadd_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Dispatch_SBM VALUES(@OrderNo,@Remarks,@Date,@IpAddress,@ETime)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@OrderNo", txtOrder.Text.Trim());
cmd.Parameters.AddWithValue("@Remarks", txtRefno.Text.Trim());
cmd.Parameters.AddWithValue("@Date", txtEntrydate.Text.Trim());
cmd.Parameters.AddWithValue("@IpAddress", lblIP.Text);
cmd.Parameters.AddWithValue("@ETime", DateTime.Now.ToString("hh:mm"));
con.Open();
int ID = Convert.ToInt32(cmd.ExecuteNonQuery());
con.Close();
if (ViewState["dt"] != null)
{
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows.Add(ID);
}
else
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("PID") });
dt.Rows.Add(ID);
ViewState["dt"] = dt;
}
this.BindGrid();
}
}
}
private void BindGrid()
{
this.gvBal.DataSource = ViewState["dt"] as DataTable;
this.gvBal.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gvBal.Rows)
{
int pid = int.Parse((row.FindControl("PID") as Label).Text);
this.InsertRows(pid);
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Record saved sucessfully'); window.open('BBDisList.aspx');", true);
}
private void InsertRows(int pid)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Dispatch_SBD VALUES(@ID,@PID)", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PID", pid);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Dispatch_SBM VALUES(@OrderNo,@Remarks,@Date,@IpAddress,@ETime)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@OrderNo", txtOrder.Text.Trim())
cmd.Parameters.AddWithValue("@Remarks", txtRefno.Text.Trim())
cmd.Parameters.AddWithValue("@Date", txtEntrydate.Text.Trim())
cmd.Parameters.AddWithValue("@IpAddress", lblIP.Text)
cmd.Parameters.AddWithValue("@ETime", DateTime.Now.ToString("hh:mm"))
con.Open()
Dim ID As Integer = Convert.ToInt32(cmd.ExecuteNonQuery())
con.Close()
If ViewState("dt") IsNot Nothing Then
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows.Add(ID)
Else
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("PID")})
dt.Rows.Add(ID)
ViewState("dt") = dt
End If
Me.BindGrid()
End Using
End Using
End Sub
Private Sub BindGrid()
Me.gvBal.DataSource = TryCast(ViewState("dt"), DataTable)
Me.gvBal.DataBind()
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvBal.Rows
Dim pid As Integer = Integer.Parse((TryCast(row.FindControl("PID"), Label)).Text)
Me.InsertRows(pid)
Next
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "alert", " alert('Record saved sucessfully'); window.open('BBDisList.aspx');", True)
End Sub
Private Sub InsertRows(ByVal pid As Integer)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Dispatch_SBD VALUES(@ID,@PID)", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PID", pid)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub