User246057125 posted
I am creating a session variable on Page_Load as below:
try
{
if (!IsPostBack)
{
Session.Add("CategoryTaskFlag", "New");
LoadPipes();
LoadCategoryTypes();
RefreshPipeLengthGridWithTempData();
}
RebindGrid();
}
catch (Exception ex)
{
reuse.CustomClientMessage(ex.Message.ToString(),this.Page);
}
Below is the code sample of a method to pass parameters to the stored procedure. I am using this session value in this procedure.
protected void SaveRecord()
{
try
{
int TwSerialNumber = 0;
if (Session["CategoryTaskFlag"].ToString() == "New")
TwSerialNumber = 0;
else if (Session["CategoryTaskFlag"].ToString() == "Update")
TwSerialNumber = Convert.ToInt32(Session["CategorySerialNumber"].ToString());
SqlCommand cmdCategoryEntry = new SqlCommand("pheSch_CreateOrUpdateTubewellTypeDetails", dbConnection.cn);
cmdCategoryEntry.Parameters.Add(new SqlParameter("@TaskFlag", SqlDbType.NVarChar));
cmdCategoryEntry.Parameters["@TaskFlag"].Value = Session["CategoryTaskFlag"].ToString();
cmdCategoryEntry.Parameters.Add(new SqlParameter("@SerialNumber", SqlDbType.BigInt));
cmdCategoryEntry.Parameters["@SerialNumber"].Value = Convert.ToInt32(txtMainSerial.Text);
cmdCategoryEntry.Parameters.Add(new SqlParameter("@pipe_type", SqlDbType.NVarChar));
cmdCategoryEntry.Parameters["@pipe_type"].Value = txtPipe.Text;
cmdCategoryEntry.CommandType = CommandType.StoredProcedure;
if (dbConnection.cn.State == ConnectionState.Open)
dbConnection.CloseConnection();
dbConnection.OpenConnection("Scheme");
cmdCategoryEntry.ExecuteNonQuery();
dbConnection.CloseConnection();
RebindGrid();
Session["CategoryTaskFlag"] = "New";
Session["CategorySerialNumber"] = "";
ClearFields();
}
catch (Exception ex)
{
CustomClientMessage(ex.Message.ToString());
}
finally
{
categorySerialNo = 0;
mainSerialNo = 0;
}
}
After a few entries, I notice that records stop inserting and sometimes Sys.WebForms.PageRequestParserException is displayed frequently until I refresh it several times to make it work.
I want to know how to handle this Session variable. Is there any other way to handle global variables in ASP.NET 2.0?