i am trying to pass last inserted id on another page,but on master detail form (If have multiple rows in gridview,just one row getting inserted into detail table and last ID get forward on other page,but i want that all rows in gridview get insert into datbase then ,id Pass on another page )
below is my C# code..
protected void btn_Save_Click(object sender, EventArgs e)
{
insertintopd();
}
private void insertintopd()
{
if (txtMRNo.Text == "")
{
Response.Write("<script>alert('Please Input MR Number')</script>");
}
else if (txtPname.Text == "")
{
Response.Write("<script>alert('Please Input Correct MR No./Register Patient.')</script>");
}
else
{
SqlCommand cmd = new SqlCommand("SP_insertOPD", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DID", DDLDoc.SelectedValue);
cmd.Parameters.AddWithValue("@RID", txtMRNo.Text);
cmd.Parameters.AddWithValue("@Sfee", 0);
cmd.Parameters.AddWithValue("@Date", DateTime.Now.ToString());
cmd.Parameters.AddWithValue("@ETime", DateTime.Now.ToString("HH:mm"));
con.Open();
Orderno = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
DataTable dt = (DataTable)ViewState["dt"];
int sid, sfee, sqty;
foreach (DataRow row in dt.Rows)
{
sid = int.Parse(row["SID"].ToString());
sfee = int.Parse(row["SFee"].ToString());
sqty = int.Parse(row["S_QTY"].ToString());
this.InsertRows(sid, sfee, sqty);
}
}
private void InsertRows(int sid, int sfee, int sqty)
{
using (SqlCommand cmd = new SqlCommand("SP_insertOPDDetail", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@OID1", Orderno);
cmd.Parameters.AddWithValue("@SFee", sfee);
cmd.Parameters.AddWithValue("@QTY", sqty);
cmd.Parameters.AddWithValue("@SID", sid);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
int ResultValue = Convert.ToInt32(Orderno);
if (ResultValue > 1)
{
Response.Redirect("PrntServ.aspx?OID=" + ResultValue + "");
}
else
{
Response.Write("<script>alert('Record Not save')</script>");
}
}
}
public object Orderno { get; set; }