how to check session is null or not

Posted by Shanky11 under ASP.NET on 3/1/2013 | Points: 10 | Views : 10913 | Status : [Member] | Replies : 9
if(Session["username"].ToString() == null)
{

Response.Redirect("LoginPage.aspx");
}
else
lblwelcome.Text = " Welcome " +Session["username"].ToString();
-----------------------------------this code i have written on home page load event

if user login in successfully and comes to this page
and after that close th tab by copying url of home page it agains open this should not happen instead it should goes to login page but this is not happening
so help me to overcome this pblm............




Responses

Posted by: Shameer.Ali87 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
@shanky11,

first you have to save the username(user credentials) in session, and then you
keep your above code in home.aspx page load event..




shameer ali shaik

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
if((txtusername.Text != " ")&&(txtpassword.Text != " "))
{
Session["username"] = txtusername.Text;
Response.Redirect("HomePage.aspx");
}
i have already written this on button click

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
If (condition) Then
Session("UserId") = _UserId
Response.Redirect("../Administration/Home.aspx", True)
Else
lblInvalidAuthen.Text = "Invalid User ID & Password"
End If

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
hello Oswaldily
i think u r not getting my pount i m pasting my code below for homeage and login page
open your gmail a/c
1.copy url of it 2.logout 3.close tab 4.Open new tab paste copied url here your home page will not open u have to login again thats the action i want to perform.............................................
login page -----------------------------------------------
if((txtusername.Text != " ")&&(txtpassword.Text != " "))
{
Session["username"] = txtusername.Text;
Response.Redirect("HomePage.aspx");
}
------------------------------------HOME PAGE
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"].ToString() == null)
{
Response.Redirect("LoginPage.aspx");
}
else
lblwelcome.Text = " Welcome " + Session["username"].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
{
if (Session["username "] != null)
Session.Remove("username");
Response.Redirect("LoginPage.aspx");
}
}

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Santhi on: 3/1/2013 [Member] Starter | Points: 25

Up
0
Down
Hi


Always you have to check like this.
Even, the session value is null . you are converting it in to a string.
There is a difference between Convert.ToString() and .ToString().
It is better to use Convert.ToString() because it will not through exception errors when you are trying to convert it
If you use .ToString() it will throw exception sometimes when you are converting to convert it.
So,
You have to check whether the session is empty or not like this.

If(Convert.ToString(Session["LoginID"]) == "" )
{
Response.Redirect("login.aspx");
}

Thanks & Regards,
Santhi .V

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/4/2013 [Member] Starter | Points: 25

Up
0
Down
http://www.dotnetfunda.com/forums/thread7377-rectify-my-login-page-error.aspx
http://www.dotnetfunda.com/forums/thread7371-solve-this-session-error.aspx

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Somasundaram on: 3/4/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

U need to use session.abandon in Logout Button.

Session.Abandon() 

Thanks,
Somasundaram

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/4/2013 [Member] Starter | Points: 25

Up
0
Down
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"].ToString() != "")
{
lblusername.Text = "Welcome" + " " + Session["Username"].ToString();
}
else
{
Response.Redirect("../Administration/login.aspx");
}
}

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Selfwilleddude on: 3/4/2013 [Member] Starter | Points: 25

Up
0
Down
There is no solution for your query..
Actually you are not closing the browser.. you just closing the tab that means session is not destroyed.. Session value is destroyed only if the browser is closed..
so provide a button and on click event call this method session.abondon()..

alternatively you can set the session timeout for 1 min ..

SESSION VALUES IS NOT DESTROYED UNTIL IT IS DESTROYED BY CODE OR BROWSER CLOSED...


if you get my point then please mark this as ur solution.. :)

HAPPY CODING....

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response