Click here to Skip to main content
15,919,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
C#
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

This is my code.
C#
private void bindimage()
{
    
    SqlDataAdapter adp = new SqlDataAdapter("select  image from display where username=@us", con);
   adp.SelectCommand.Parameters.AddWithValue("@us", Session["ss"].ToString());
   // SqlDataAdapter ado = new SqlDataAdapter();
  // adp.SelectCommand = new SqlCommand("select  image from display where username='@us'", con);
 //  adp.SelectCommand = new SqlCommand();
    //adp.SelectCommand.Parameters.AddWithValue("@us",(Session["ss"]).ToString());
    DataSet ds = new DataSet();
    adp.Fill(ds);
    DataList2.DataSource = ds;
    DataList2.DataBind();
}

The error comes from this line as follow:
C#
Error line: adp.SelectCommand.Parameters.AddWithValue("@us", Session["ss"].ToString());
Posted
v2
Comments
Hitesh_Mistry 2-Jun-13 1:18am    
have you checked the value in the session variable..???
Swayam231 2-Jun-13 4:17am    
well replace addwith to add and write this
("@us", sqldbtype.varchar).value= session["ss"].tostring

Let me know if it heps

Before using any session variable you should check for it's existence. If that session is NULL then definitely it'll show Null Reference Exception. Try this:
C#
private void bindimage()
{    
    if(Session["ss"] != null){
        SqlDataAdapter adp = new SqlDataAdapter("select  image from display where username=@us", con);
        adp.SelectCommand.Parameters.AddWithValue("@us", Session["ss"].ToString());
        DataSet ds = new DataSet();
        adp.Fill(ds);
        DataList2.DataSource = ds;
        DataList2.DataBind();
    }
    else{
        Response.Write("Session is null"); // You can remove this and show any kind of message to user.
    }
}



--Amit
 
Share this answer
 
It seems the "ss" does not have any value in Session. Everything else looks fine.
 
Share this answer
 
Comments
[no name] 2-Jun-13 2:03am    
no i have n't check ss.how to fix this error.Please suggest me.
Pankaj.Sinha.Techno 2-Jun-13 2:13am    
Since while addding parameter to command you are fetching a value from session using string index "ss". Please make sure that this value is being added into session before that.

replcae line
adp.SelectCommand.Parameters.AddWithValue("@us", Session["ss"].ToString());
with
if (Session["ss"] != null)
{
adp.SelectCommand.Parameters.AddWithValue("@us", Session["ss"].ToString());
}

and also since you are the one who is responsible to add this value into session in the code make sure you are doing that.
[no name] 2-Jun-13 2:43am    
Thank you so much for your suggestion But an another error come with a message
Must declare scalar variable "us".what can i do for this?Please help me
Calvijn 2-Jun-13 10:53am    
It says that your variable "@us" in "SqlDataAdapter adp = new SqlDataAdapter("select image from display where username=@us", con);"

is not declared.
Something like "DECLARE @us VARCHAR(128)"
also you have to set a value for that variable like:
"SET @us = 'WHAT EVER YOU SET'

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