Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 47:         //}
Line 48:         //Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
Line 49:         SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["techdata"].ToString());
Line 50:         SqlCommand cmd = new SqlCommand();
Line 51:         try


The orignal code is:

public partial class ProductDetails : System.Web.UI.Page
{
    //string yourname = TextBox1.Text.ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["cart"] != null && Session["cart"] != "")
        {
            yourname.Visible = false;
        }
        else
        {
            yourname.Visible = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
        string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
        string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
        string ProductID = ((HiddenField)DataList1.Controls[0].FindControl("HiddenField1")).ToString();
        //int ProductID;
        //if (Request.QueryString["ProductID"] != null) 
        //    ProductID = int.Parse(Request.QueryString["ProductID"].ToString());
        //if (Profile.SCart == null)
        //{
        //    Profile.SCart = new ShoppingCartExample.Cart();
        //    //Session["cart"] = SCart;
        //}
        //if (Session["cart"] != null)
        //{
        //    ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('CArt is available');</script>");
        //}
        //Profile.SCart.Insert(ProductID, Price, 1, ProductName, ProductImageUrl);
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["techdata"].ToString());
        SqlCommand cmd = new SqlCommand();
        try
            {
            conn.Open();
            cmd = conn.CreateCommand(); 
            if (cmd != null) { cmd.Dispose(); }
            cmd.CommandText = "INSERT INTO cart (ProductID,CustomerName, ProductName, " +
            "ProductImage, ProductPrice) VALUES ('" + ProductID + "','" + yourname.Text.ToString() + "','" + ProductName + "','" +
            ProductImageUrl + "','" + Price + "')";
            cmd.ExecuteNonQuery();
            conn.Close();
            Response.Redirect("~/Admin_Control/UpdateProductInfo.aspx");
            }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('" + ex.ToString() + "');</script>");

        }
        //ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Product added successfully');</script>");
        Session["cart"] = yourname.Text.ToString();
        Server.Transfer("product.aspx");
    }
}
Posted
Updated 24-Feb-10 16:25pm
v3

Let me guess: There is no techdata in the application settings?
 
Share this answer
 
Based on the PageLoad and Button1_Click methods, possible places for this error:

1. The way you had coded here - LOGICAL mistake:

C#
if (cmd != null) { cmd.Dispose(); }
    cmd.CommandText = &quot;INSERT INTO cart (ProductID,CustomerName, ProductName, &quot; +
            &quot;ProductImage, ProductPrice) VALUES ('&quot; + ProductID + &quot;','&quot; + yourname.Text.ToString() + 
&quot;','&quot; + ProductName + &quot;','&quot; +
            ProductImageUrl + &quot;','&quot; + Price + &quot;')&quot;;


If there is a command cmd, you go ahead and 'Dispose' it. After that you continue your execution. Every time you create command you dispose it and then try to access the same 'cmd' for Sql execution.
You should use, try-catch-finally for such sql commands and transactions. In finally check for the cmd value to dispose it.


2. This is based on the hardcoded strings used - TYPO mistake, if any of them are not existing then you get an error at runtime:
C#
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("PriceLabel")).Text);
        string ProductName = ((Label)DataList1.Controls[0].FindControl("NameLabel")).Text;
        string ProductImageUrl = ((Label)DataList1.Controls[0].FindControl("ImageUrlLabel")).Text;
        string ProductID = ((HiddenField)DataList1.Controls[0].FindControl("HiddenField1")).ToString();
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["techdata"].ToString());
 
Share this answer
 
I assume you're writing this for fun, because not only is it badly written, but it's definitely very insecure. You shouldn't dive this deep into a fun, learning type project, without first buying some books and reading them.
 
Share this answer
 
Please mention the exact line that throws the error.

And also, format your code when you paste it in here, makes for easier reading.
 
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