Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server Error in '/' Application.

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 34:         }
Line 35: 
Line 36:         string strcon = ConfigurationManager.AppSettings["payrollConnectionString"].ToString();
Line 37:         protected void Page_Load(object sender, EventArgs e)
Line 38:         {

Source File: C:\Users\Surya\Documents\Visual Studio 2008\Projects\Surya\imageexport\imageexport\Default.aspx.cs    Line: 36 


Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
   imageexport._Default..ctor() in C:\Users\Surya\Documents\Visual Studio 2008\Projects\Surya\imageexport\imageexport\Default.aspx.cs:36
   ASP.default_aspx..ctor() in c:\Users\Surya\AppData\Local\Temp\Temporary ASP.NET Files\root\d163ef2f\853db724\App_Web_s6dguo8s.0.cs:0
   __ASP.FastObjectFactory_app_web_s6dguo8s.Create_ASP_default_aspx() in c:\Users\Surya\AppData\Local\Temp\Temporary ASP.NET Files\root\d163ef2f\853db724\App_Web_s6dguo8s.2.cs:0
   System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
   System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
   System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +160
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155


I got this error.
Can anyone help me?

My .cs code is:
C#
namespace imageexport
{
    public partial class _Default : System.Web.UI.Page
    {
        class Problem
        {
            static void Main()
            {
                Problem problem = null; // problem has not been instantiated
                // ...
            }

            public int ProblemProperty
            {
                get { return 0; }
            }

            public string ProblemMethod()
            {
                return null;
            }
        }

        string strcon = ConfigurationManager.AppSettings["payrollConnectionString"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridData();
            }

        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //Condition to check if the file uploaded or not
            if (fileuploadImage.HasFile)
            {
                //getting length of uploaded file
                int length = fileuploadImage.PostedFile.ContentLength;
                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];
                //store the currently selected file in memeory
                HttpPostedFile img = fileuploadImage.PostedFile;
                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);
                string imagename = txtImageName.Text;
                //use the web.config to store the connection string
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection);
                cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
                cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                    BindGridData();
                    txtImageName.Text = string.Empty;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
                }
            }
        }
        /// <summary>
        /// function is used to bind gridview
        /// </summary>
        private void BindGridData()
        {
            SqlConnection connection = new SqlConnection(strcon);
            SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            gvImages.DataSource = dt;
            gvImages.DataBind();
            gvImages.Attributes.Add("bordercolor", "black");
        }
    }
}

My handler .ashx Code is:
C#
namespace imageexport
{
    public partial class _Default : System.Web.UI.Page
    {
        class Problem
        {
            static void Main()
            {
                Problem problem = null; // problem has not been instantiated
                // ...
            }

            public int ProblemProperty
            {
                get { return 0; }
            }

            public string ProblemMethod()
            {
                return null;
            }
        }

        string strcon = ConfigurationManager.AppSettings["payrollConnectionString"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridData();
            }

        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //Condition to check if the file uploaded or not
            if (fileuploadImage.HasFile)
            {
                //getting length of uploaded file
                int length = fileuploadImage.PostedFile.ContentLength;
                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];
                //store the currently selected file in memeory
                HttpPostedFile img = fileuploadImage.PostedFile;
                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);
                string imagename = txtImageName.Text;
                //use the web.config to store the connection string
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Image (ImageName,Image) VALUES (@imagename,@imagedata)", connection);
                cmd.Parameters.Add("@imagename", SqlDbType.VarChar, 50).Value = imagename;
                cmd.Parameters.Add("@imagedata", SqlDbType.Image).Value = imgbyte;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                    BindGridData();
                    txtImageName.Text = string.Empty;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);
                }
            }
        }
        /// <summary>
        /// function is used to bind gridview
        /// </summary>
        private void BindGridData()
        {
            SqlConnection connection = new SqlConnection(strcon);
            SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [Image]", connection);
            SqlDataAdapter daimages = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            daimages.Fill(dt);
            gvImages.DataSource = dt;
            gvImages.DataBind();
            gvImages.Attributes.Add("bordercolor", "black");
        }
    }
}

Can anyone help me?
How to clear this error?
Posted
Updated 5-Mar-12 22:01pm
v2
Comments
André Kraak 6-Mar-12 4:01am    
Edited question:
Added pre tags

1 solution

Split the line:
C#
string strcon = ConfigurationManager.AppSettings["payrollConnectionString"].ToString();
Leave half where it is:
C#
string strcon;
Move the rest to the top of your Page_Load event:
C#
strcon = ConfigurationManager.AppSettings["payrollConnectionString"].ToString();
 
Share this answer
 
Comments
v surya dev 6-Mar-12 3:47am    
ya... i did... but... now also i am getting the same error....?
OriginalGriff 6-Mar-12 4:02am    
Then you need to check it.
Put a breakpoint on it, and look at each bit - find the null!
A thought occurs - have you stored the connection strings in the right place in web.config? Normally, I would access mine via
ConfigurationManager.ConnectionStrings["LoggingDatabase"].ConnectionString
rather than via AppSettings
v surya dev 6-Mar-12 3:50am    
plz help me....
v surya dev 6-Mar-12 4:06am    
really great pa... very very thanks to you... now it's working... thanks a lot....
OriginalGriff 6-Mar-12 6:35am    
You're welcome!

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