Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
public class ImageHandler : IHttpHandler {
    string strcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
    
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        string imageid = context.Request.QueryString["ImID"];
        SqlConnection connection = new SqlConnection("Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True");
        connection.Open();
        SqlCommand command = new SqlCommand("select Image from Image1 where ImageId=" + imageid, connection);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        connection.Close();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}




error..........

Object reference not set to an instance of an object.
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.

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

Source Error:
Line 7:  
Line 8:  public class ImageHandler : IHttpHandler {
Line 9:      string strcon = ConfigurationManager.AppSettings["ConnectionString"].ToString();
Line 10:     
Line 11:     public void ProcessRequest (HttpContext context)
Posted
Updated 16-Feb-12 19:11pm
v2
Comments
Varun Sareen 17-Feb-12 1:12am    
edit for : added pre tag
rockpune 17-Feb-12 1:19am    
am newly learnig .net sir plz tell me how to add pre tag
Varun Sareen 17-Feb-12 1:33am    
Dear Friend, Where ever you start your code then start with <pre> and when ends the code then end it with </pre>
Sanjay K. Gupta 17-Feb-12 2:17am    
Rock..
see web.confing->appSettings key

1 solution

Dear Friend,

An object reference not set to an instance of an object comes whenever you are using or assigning a variable to something which is not yet initialized and what i get from your line of code error is that your connection string

ConfigurationManager.AppSettings["ConnectionString"].ToString();


is blank as you may not have initialized it in the webconfig file.

So try to check there you may get the solution there.

If it helps you out then mark this as your answer and i am open to queries.

Thanks
 
Share this answer
 

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