How to use sessions to send contents from one page to other in ASP.NET


Here i am going to explain how one can send the values/content of textbox/labels to the other page through query string or Session in ASP.NET. This may help in many situations like in medical shop where doctor will save the details on one page and he/she may send the drug list to his/her assistant by this method.

Here I am going to explain how one can send the values/content of textbox/labels to the other page through query string or session in ASP.NET. This may help in many situations like in medical shop where doctor need to save the details on one page and he/she may send the drug list to shop by this method. Suppose a scenario where, a patient has reached out to doctor for the check up. Doctor will treat him/her and doctor will fill all the details like demographic details, drug details etc and he will save that form. Now Doctor needs to ask his assistant for the drug in this case doctor only need to send the medicine name. Here we can use query string. From the prescription page doctor will send the name of the drug and on the medicine page his assistant will receive the same. This can be done so easily as below-

Suppose doctor saves 5 medicine name as- med1,med2,med3,med4,med5 and patient name as well as id on the prescription page as below-


cmd.Parameters.AddWithValue("@Prescription1", Pres1.Text);

cmd.Parameters.AddWithValue("@Prescription2", Pres2.Text);

cmd.Parameters.AddWithValue("@Prescription3", Pres3.Text);

cmd.Parameters.AddWithValue("@Prescription4", Pres4.Text);

cmd.Parameters.AddWithValue("@Prescription5", Pres5.Text);

cmd.Parameters.AddWithValue("@Name", name.Text);

cmd.Parameters.AddWithValue("@dies", des.Text);

Now once the details are saved in the database, doctor need to send these details to his assistant on drug page. Than he will first send the details from his page like below-

Session["med1"] = Pres1.Text;

Session["med2"] = Pres2.Text;

Session["med3"] = Pres3.Text;

Session["med4"] = Pres4.Text;

Session["med5"] = Pres5.Text;

Session["name"] = name.Text;

Session["des"] = des.Text;

Here i have sent all the details through session.
Now on the drug page, receive all these values at the page load like below-

string nam = Session["name"].ToString();

string des = Session["des"].ToString();

string me1 = Session["med1"].ToString();

string me2 = Session["med2"].ToString();

string md3 = Session["med3"].ToString();

string md4 = Session["med4"].ToString();

string md5 = Session["med5"].ToString();

Now define as many label on the drug page as needed for these values; In this case i need 7 labels , define them as below-

<asp:Label ID="medi8" runat="server" Text=""></asp:Label>


Similarly define others also.

And now define the value we have received from prescription page to these labels like below-

medi1.Text = me1.ToString();

medi2.Text = me2.ToString();

medi3.Text = md3.ToString();

medi4.Text = md4.ToString();

medi5.Text = md5.ToString();

name.Text = nam.ToString();

dis.Text = des.ToString();

That's all. With this you have received all the values through Query string and sessions on the other page !!


Comments

Author: naveensanagasetti24 Jul 2014 Member Level: Gold   Points : 2

Hi,

Nice explanation, but before assign the session object to controls you must check that session is expire or not, why means once session is expire if you are trying to assign null object to control then it's throwing runtime error.


EX:


string name="";
if(Session["name"]!=null)
name = Session["name"].ToString();

Author: Ashutosh Jha24 Jul 2014 Member Level: Gold   Points : 0

Yes Naveen. Agree with you !!

Thanks for the suggestion.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: