1

I am getting wrong .Plz anyone help

** System.NullReferenceException: Object reference not set to an instance of an object.

//Line 35: SqlCommand cmd = new SqlCommand("Select * UserInfm where UserName = '" + Request.Cookies["Login"]["UserName"].ToString() + "' ", con);

 if(!IsPostBack)

    {
        if( Session["Login"] == null && Request.Cookies["Login"]==null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {

            string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {



                SqlCommand cmd = new SqlCommand("Select * UserInfm where UserName = '" + Request.Cookies["Login"]["UserName"].ToString() + "' ", con);
                cmd.CommandType = CommandType.StoredProcedure;
                 con.Open();
                 using (SqlDataReader rdr = cmd.ExecuteReader())
                 {
                     while (rdr.Read())
                     {
                       LabelUN.Text = rdr["UserName"].ToString();
                     }
                 }
                 con.Close();
            }


            Response.ClearHeaders();
            Response.AddHeader("Cache-Control", "no-cache,no-store,max-age=0,must-revalidate");
            Response.AddHeader("Pragma", "no-cache");

        }
    }

}
Raju
  • 622
  • 6
  • 10

2 Answers2

0

Request.Cookies["Login"]["UserName"] could be the issue in your case. Please watch the value of this code while debugging. This might be null and as you are using .ToString() it can throw Null Reference Exception. Another thing the command is Select * from UserInfm - you have not specified 'from'

Sujith
  • 1,604
  • 9
  • 16
0

First check value of Request.Cookies["Login"] and Request.Cookies["Login"]["UserName"] in quick watch. Add check of null in if condition on it.

if(Request.Cookies["Login"] != null AND Request.Cookies["Login"]["UserName"].ToString() != null)
{
 /// Your implementation.
}                                                                           

FROM is missing in your query. Add FROM like Select * FROM UserInfm

Yogesh Patel
  • 818
  • 2
  • 12
  • 26