Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Can anyone know?

Object reference not set to an instance of an object. How to solve this error?

Coding:
C#
protected void btncart_Click(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        int Quantity = Convert.ToInt32(0);
        if (Quantity == 0)
        {
            Quantity = 1;
        }
        else
        {
            Quantity = Quantity + 1;
        }
        mas_id();
        string pro_id = (((Label)DataList1.Controls[0].FindControl("pro_id")).Text).ToString();
        string pro_name = (((Label)DataList1.Controls[0].FindControl("pro_name")).Text).ToString();
        float Price = float.Parse(((Label)DataList1.Controls[0].FindControl("Price")).Text);
        string pro_img = ((Image)DataList1.Controls[0].FindControl("pro_img")).ImageUrl;
        //int quantity = Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtquantity")).Text);
        string master_id = (((Label)DataList1.Controls[0].FindControl("master_id")).Text).ToString();//error comes here
        SqlCommand cmd = new SqlCommand("insert into addcart values('" + master_id + "','" + pro_id + "','" + pro_name + "','" + Quantity + "','" + Price + "','" + pro_img + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
Posted
Updated 25-Nov-11 18:26pm
v3
Comments
RaisKazi 26-Nov-11 0:22am    
Possible Reason - null reference.
Possible Resolution - Please show us your code.
RaisKazi 26-Nov-11 0:27am    
Edited: Added "pre" tag.
Voted 4 as its not a too bad Question.
_Tushar Patil 26-Nov-11 5:03am    
Put Break point Check Which Line Giving Error......?
shahviral 1-Feb-12 1:09am    
Have You Find a solution ?I need a code for datalist.I have to bind dropdownlist in datalist but i can't..can you help me with your code?
sathiyak 1-Feb-12 1:27am    
this is the code for bind data's in datalist....


public void datalist()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select pro_id,pro_name,pro_img,price from product where status='A'and type='retail'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
con.Close();
}

The object is null. please initialize it. for example object o=new object();
 
Share this answer
 
Comments
sathiyak 26-Nov-11 0:26am    
cant understood.........
perheps (Label)DataList1.Controls[0].FindControl("master_id") returns null instead of your expected Lebel control.
 
Share this answer
 
v3
Multiple possibilities for this Error in your code. One of below might having null value.

1) con
2) DataList1
3) DataList1.Controls[0]
4) DataList1.Controls[0].FindControl("pro_id")

Same applicable for other lines of FindControl.

Debug your code and check what is having null value and then declare and initialize it properly.
 
Share this answer
 
try this

 Label lbl = (Label)DataList1.Controls[0].FindControl("master_id")).Text).ToString();
string master_id = lbl.Text;



yogesh.
 
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