Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,

My default page is Site.master page & i have written the code in my Site.Master is bellow:
====================
C#
protected void Page_Load(object sender, EventArgs e)
       {

           if (!String.IsNullOrEmpty(Session["userName"].ToString()))
           ////if (Session["UserName"] != null)
           {

           lblUName.Text = "Welcome" + Session["userName"].ToString();

           //    //lnkLogout.Visible=true;
           }
           else
           {
               Response.Redirect("Login.aspx");
           }

           }


===============

Ans i also write the code in my Login.aspx.cs page is bellow for Session..
===============
C#
if (result >= 1)
           {
               Session["userName"] = txtUserName.Text.ToString().Trim();
               Response.Redirect("Site.master");
           }
           else
               lblMsg.Text = "Incorrect Username or Password";


==============

Now when i run the program it shown the error mess bellow::

Object reference not set to an instance of an object.

please help me where is my mistake???
Posted
Comments
amperayani 15-Apr-13 1:48am    
write like this

if(session["username"]!=null)
prodipjsr 15-Apr-13 1:56am    
Whine i use ur code it shown the error mess::
The page isn't redirecting properly

Try the If condition like, because .ToString() method cant handle null value
if (!String.IsNullOrEmpty(Convert.ToString(Session["userName"])))
{
  // your code
}
 
Share this answer
 
string.IsNullOrEmpty[^] tests strings. It checks for string references that are null, or empty strings. Strings are reference types and can be equal to null like any other reference type.
It is equivalent to result = SomeString == null || SomeString == String.Empty.

Since your code
Quote:
if (!String.IsNullOrEmpty(Session["userName"].ToString()))

Here you are already converting the Session["userName"] to string using ToString() function. In this case if the session is null it'll give NullReferanceException, because a null value cannot converted to string.

Try this:
C#
if (Session["UserName"] != null)
{
    string s = "Welcome" + Session["userName"].ToString();
    //    //lnkLogout.Visible=true;
}
else
{
    Response.Redirect("Login.aspx");
}



--Amit
 
Share this answer
 
amperayani is correct use if(session["username"]!=null) in master.

Why u r redirecting to master page from login page?
You should redirect to .aspx page.
 
Share this answer
 
Comments
prodipjsr 15-Apr-13 2:26am    
please let me clear what the meaning of redirect.aspx page??? i cant understand where i should write what??? please give me a code with example..
C#
qryUserName = "SELECT user_name,password FROM Login WHERE user_name ='" + username + "' AND password ='" + Password+ "'";
SqlCommand cmd = new SqlCommand(qryUserName, con);
string CurrentName;
AuthenticationName = (string)cmd.ExecuteScalar();
if (AuthenticationName != null)
{
Session["Authentication"] = username;
Session.Timeout = 1;
Response.Redirect("site.aspx");
}
else
{
Session["Authentication"] = "";
}


Try To Redirect on .aspx not on .Master
 
Share this answer
 
Comments
prodipjsr 15-Apr-13 2:26am    
please let me clear what the meaning of redirect.aspx page??? i cant understand where i should write what??? please give me a code with example..
HTML-CSS 15-Apr-13 3:19am    
Its Simple Bro..

Download this demo

https://www.dropbox.com/s/4xul8wpcbpdeooe/672Login.rar

Just Look code There is No DB so may bee you cant Login
This error occurs when you are trying to access an attribute of an object when the object itself is null.
Debugging your code will help you understand where the error occurs.
 
Share this answer
 
Comments
prodipjsr 15-Apr-13 2:27am    
i cant understand where i should write what??? please give me a code with example..

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