如何正确使用会话? [英] how to use session properly?

查看:75
本文介绍了如何正确使用会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI。有人能帮助我吗?我正在尝试将我的数据库中的字段(数据)放入我的Web应用程序中的标签。并且我在会话部分(粗体和带下划线的部分)中出现错误,说对象引用未设置为对象的实例。我不知道该怎么办。这是我的代码:



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:

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();
            }









我试图把喜欢..当页面加载时,

有说明......



WELCOME [这里显示的是用户名]

请帮助。谢谢!





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

"WELCOME [here appears the name of the user]"
Please Help. Thanks!

推荐答案

因为你还没有设置会话变量。

只做一件事。在textbox1中创建一个带有用户名的登录页面

创建一个Session变量然后

然后Session [username] = textbox1.text;

然后当你重定向到点击登录时该page_load事件中的其他页面



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

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

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



希望有帮助...


Hope it helps...


这篇关于如何正确使用会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆