ASP.Net Connection String Error: object reference not set to an instance of an object

Last Reply 7 months ago By pandeyism

Posted 7 months ago

Dear sir,

I tried your provided code. but in the productdetails page are one error. from product page has goes to productdetails page

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {            
            id = Convert.ToInt32(Request.QueryString["itemid"]);
            DataTable dt = GetData(id);           

            if (Session["Data"] != null)
            {
                DataTable dx = (DataTable)Session["Data"];
                dx.Merge(dt);
                Session["Data"] = dx;
                GridView1.DataSource = dx;
                GridView1.DataBind();
            }
            else
            {
                Session["Data"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            
        }
        private DataTable GetData(int id)
        {

            string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
            string a = "select itemname,image,price,obqty from tbproduct where id='" + @id + "'";
            SqlCommand cmd = new SqlCommand(a);
            using (SqlConnection con = new SqlConnection())
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@id", id);
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Products.aspx");
        }

error are below

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 134:        {
Line 135:
Line 136:            string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
Line 137:            string a = "select itemname,image,price,obqty from tbproduct where id='" + @id + "'";
Line 138:            SqlCommand cmd = new SqlCommand(a);

Source File: C:\Users\Dell Vostro\Documents\Visual Studio 2010\Projects\VMOSW\VMOSW\User\Productdetails.aspx.cs    Line: 136 

Stack Trace: 

 

You are viewing reply posted by: pandeyism 7 months ago.
Posted 7 months ago Modified on 7 months ago

Hi rajeesh,

rajeesh says:
string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;

Please check in your web.config file for connection string with name constring is present or not.