just trying to learn asp.net
i am able to login and redirect to default.aspx.
i am trying to check if user’s usere name and password are right from database(able to do).
now i am trying to store user name in session(not able to do, here i am getting nullpointer/value error) and show this session value as welcome message on next page/redirected page.my code is as:
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader dr;
SqlConnection con = new SqlConnection("Data Source=xxxx-pc\\ddd;Initial Catalog=db_Login;User Id=sd;Password=ds;");
con.Open();
SqlCommand com = new SqlCommand("select password from tab_userinformation where Username='" + txt_uname.Text + "'", con);
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
if (dr[0].ToString() == txt_pwd.Text)
{
Response.Redirect("default_Page.aspx");
//Response.Redirect("erp.htm");
Session["user"] = txt_uname.Text;
here i am getting object reference not set to an instance of an object exception for Session[“user”]
any suggestion
Thanks!
Store the value in your Session before the Redirect:
That way you don’t lose the value of txtuname.Text.
Also, you should use parameterized queries for your SQL – the way you currently have it, you are open to SQL Injection Attacks.