Click here to Skip to main content
15,893,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI. Can somebody help me? I'm trying to put a field(data) from my database to a label in my web app. and I get an error in the session part(the bolded and underlined one) saying "Object reference not set to an instance of an object." I don't know what to do. here is my code:

C#
string ConnString = "Server=SAMSUNG-PC;Database=ISPROG3MP;INTEGRATED SECURITY=SSPI";
        SqlConnection scnMP;
scnMP = new SqlConnection(ConnString);

            if (!this.IsPostBack)
            {
                scnMP.Open();

                //get userid from session
                ID = Session["StudentID"].ToString();


                //get the username from DB
                string getUserName = "SELECT FirstName FROM Students ";
                getUserName += "WHERE StudentID = " + ID;

                SqlCommand scmUsers = new SqlCommand(getUserName, scnMP);
                SqlDataReader sdrUsers = scmUsers.ExecuteReader();
                sdrUsers.Read();

                string strUserName = sdrUsers["UserName"].ToString();

                //load username in label: welcome <username>
                lblName.Text = strUserName;
                sdrUsers.Close();

                //select user profile
                string strProfile = "SELECT FirstName, LastName, School, Course ";
                strProfile += " FROM Students ";
                strProfile += " WHERE StudentID = " + ID;

                SqlCommand scmProfile = new SqlCommand(strProfile, scnMP);
                SqlDataReader sdrProfile = scmProfile.ExecuteReader();


                if (sdrProfile.HasRows)
                {
                    sdrProfile.Read();
                    //load user profile
                    lblFN.Text = sdrProfile["FirstName"].ToString();
                    lblLN.Text = sdrProfile["LastName"].ToString();
                    lblS.Text = sdrProfile["School"].ToString();
                    lblC.Text = sdrProfile["Course"].ToString();
                }

                scnMP.Close();
            }




I'm trying to put like.. when the page load,
There states that..

"WELCOME [here appears the name of the user]"
Please Help. Thanks!
Posted
Updated 8-Jul-13 6:36am
v2
Comments
[no name] 8-Jul-13 11:48am    
You would have to set Session["StudentID"] to be some value before it would be not null.

1 solution

Its because you have not set the session variable.
Just do one thing.make a login page with username in textbox1
Create a Session variable then
Then Session["username"]=textbox1.text;
Then on click of login when you redirect to other page in that page_load event put

C#
page_load()
{
if(Session["username"]!=null)
{
//Your 2nd page code here whatever you want to write
}

else
{
Response.Redirect("Login.aspx");
}
}

Hope it helps...
 
Share this answer
 
v2

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