Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Anyone know how to solve?


Object reference not set to an instance of an object.


code:
C#
protected void ImageButton1_Click5(object sender, ImageClickEventArgs e)
    {
        int chkusers;
        string loginemailid, password;
        loginemailid = txtusername.Text.Trim();
        password = txtpw.Text.Trim();
        Session["username"] = loginemailid;
        Session["password"] = password;
        cn.Open();
        SqlCommand cmd = new SqlCommand("select loginemailid,password from tbl_register where loginemailid='" + txtusername.Text + "' and password='" + txtpw.Text + "'", cn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                loginemailid = dr["loginemailid"].ToString();
                password = dr["password"].ToString();
                string userid = Request.QueryString["ids"];
                Session["userid"] = userid.ToString();
                Response.Redirect("Home.aspx?ids=" + userid);
            }
            dr.Close();
        }
        else 
        {
            lblmsg.Text = "Invalid Username and Password";
        }
      
    }
Posted
Updated 16-Feb-12 19:58pm
v2

The problem is

cn.Open();

You're directly opening a connection but may be you've just initialized cn Object
before this line use

SqlConnection cn=new SqlConnection(connstr);
 
Share this answer
 
Comments
zyck 17-Feb-12 2:16am    
maybe this is the solution
nice analyzation
Syed Salman Raza Zaidi 17-Feb-12 2:27am    
why down voted?????????????
C#
string userid = Request.QueryString["ids"];
Session["userid"] = userid.ToString();
userid
 might be null here
use Session["userid"] =Convert.ToString(userid) 
 
Share this answer
 
This is null reference error. Check whether the Sql Connection cn is initialised.

SqlConnection cn=new SqlConnection(connstr);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900