Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a serious bug. I have some dates that i maintain in a session from some page. dan on the page load of other page i use that session to fill the text box and below the texbox download button is there. The problem is that if the user changes the date in text box and than clicks on the download button for that date still the date that i am getting is which was in session. That is I am not able to change the dates once session has filled the text box. Although I am clearing the session and removing it on button click event still... Can u plzz tell me wat m missing ?
here is my code:-
C#
protected void btnDwnload_Click(object sender, EventArgs e)
    {
        Session["str_Single_Date"] = null;
        Session.Remove("str_Single_Date");

        Session["str_Single_Date"] = null;
        Session.Remove("str_Single_Date");

        
        string strData = txtDataType.Text;
        string strDataType = strData.ToString().ToUpperInvariant().Trim();
        DateTime From = Convert.ToDateTime(txtFrom.Text.Trim()); <--- here i m getting the same dates always
        DateTime To = Convert.ToDateTime(txtTo.Text.Trim());  <--- here i m getting the same dates always
        string strComb = txtCombi.Text;
        string strProd = txtProd.Text;
        string strHSCODE = txtHSCode.Text;
       

        int iResult = bl_Query_for_admin.Query_Data(strDataType, From, To,strHSCODE, strComb, strProd);
        if (iResult == 1)
        {
            lblMsg.Visible = false;
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.Text = "Invalid Query Formation Resulted Into No Data Generation !";
        }

    }
Posted

Stop loading the textbox with the dates in your Page_load event except when IsPostBack[^] is false!

When your user presses a button, your application is started, and the Page Load event handler is called. If you do not check in the handler if this is a Postback, you load the values from the Session into the TextBox, and overwrite any values the user had entered. The Button Click event handler is then called, which reads the overwritten values from the text boxes.

So what it looks like is that the session values are not being deleted - they are, but just after you have used the values to overwrite the user input!
 
Share this answer
 
Comments
Taresh Uppal 24-May-12 4:06am    
if i wont do it..than info will be missing..than how would text box be automatically populated the very first time when the user visits the page ?? I understand your point but doing it is the only way i guess...
OriginalGriff 24-May-12 4:11am    
Trust me, it's the way things work. In your Load event handler, you need to check if this is a postback - if it is a postback then it's the first time load and you should use the session to load your boxes. If it isn't, then it's a load as a reposnse to user activity on the page and you shouldn't!

[edit]Typo: "isn't" twice, instead of "isn't" and "is" :O - OriginalGriff[/edit]
You need to look at how web pages work in a bit more detail, or this kind of thing is going to bite you repeatedly, and slow you down a lot! Web development is very, very different from desktop - there are at least two separate processing systems involved, instead of one and this means things work differently.
Taresh Uppal 24-May-12 5:02am    
thanx a lot for your response... but when i am not loading the text boxes on page load..means (!ispostback)....coz when i do it ..than on button click when i try to retrieve the text box value it says it is not a valid date time string...:(
Taresh Uppal 24-May-12 5:18am    
this happens on my page load from wher text box is populated very first tym:
if (!IsPostBack)
{
pn_Details_Particular_Query.Visible = false;
lblMsg.Visible = false;

}

if (Session["Client_Particular_Query_Details"] != null)
{
DataTable dtDetails = (DataTable)(Session["Client_Particular_Query_Details"]);
if((dtDetails != null)||(dtDetails.Rows.Count !=0))
{
fill_Query_fields_Particular_Query(dtDetails);
}

}


and the button coding i have shown u...please reply
OriginalGriff 24-May-12 5:40am    
You need to extend the if condition to cover all of that code - at the moment it only covers two lines...
this is the syntax for removing session
Session.Remove("SessionName");

you fill the textbox data by session value
C#
protected void Page_Load(object sender, EventArgs e)
    {

// assign textbox value to session on postback 
if (IsPostBack)
        {
            Session["SessionName"] =TextBox1.text;
        }
/*
if is not post back and open first time page then assign textbox value from session
*/

if (!IsPostBack)
{
TextBox1.text=Session["SessionName"].ToString();
}

}


if you do code in page load event like i describe above than you don't need to remove session.
 
Share this answer
 
will anybody help me on this ?
 
Share this answer
 
Comments
mr.priyank 25-May-12 3:20am    
where are u assigning the value of textboxes from session?
Taresh Uppal 25-May-12 3:56am    
on the page load but outside if(!ispostback)...
Taresh Uppal 25-May-12 3:57am    
There is a reason for this coz next tym when the page loads then i dnt want all the fields to change ...i have datefrom and dateTo text boxes whose value i want to change as per user request...
if i kp the thngs in postback than on secnd tym load all the values go empty...:(
Prosan 26-May-12 6:16am    
what's happen is your problem sort out or not. if not where you got stuck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900