Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in the following code there is an error in line
C#
da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"].ToString()) + " ", con);

the error shown is-
C#
"System.NullReferenceException: Object reference not set to an instance of an object."
...please help me sove this error
C#
public partial class user_in_box_creditordebitcard : System.Web.UI.Page
{
    SqlConnection con;
    String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        using (con = new SqlConnection(strConnection))
        {
 
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
 
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        ArrayList arr = new ArrayList();
 

 
        if (RadioButton1.Checked == true)
        {
            using (con = new SqlConnection(strConnection))
            {
 
                da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"].ToString()) + " ", con);
                ds = new DataSet();
                da.Fill(ds, "tbl_tatc");
                if (ds.Tables["tbl_tatc"].Rows.Count > 0 && ds.Tables.Count > 0)
                {
                    for (int i = 0; i < ds.Tables["tbl_tatc"].Rows.Count; i++)
                    {
                        arr.Add(ds.Tables["tbl_tatc"].Rows[i][0].ToString());
                    }
                    if (arr.Count > 0)
                    {
                        for (int j = 0; j < arr.Count; j++)
                        {
                            da = new SqlDataAdapter("select bname,imgpath,cost from tbl_addtocart where cartid=" + arr[j] + " ", con);
                            ds = new DataSet();
                            da.Fill(ds, "tbl_tatc");
                            if (ds.Tables.Count > 0 && ds.Tables["tbl_tatc"].Rows.Count > 0)
                            {
                                da = new SqlDataAdapter("insert into tbl_soldproducts(uid,brandname,imgpath,cost,cdate)values(" + Convert.ToInt32(Session["uid"].ToString()) + ",'" + ds.Tables["tbl_tatc"].Rows[0][0].ToString() + "','" + ds.Tables["tbl_tatc"].Rows[0][1].ToString() + "'," + Convert.ToDecimal(ds.Tables["tbl_tatc"].Rows[0][2].ToString()) + ",getdate())", con);
                                da.SelectCommand.ExecuteNonQuery();
                            }
                        }
 
                        da = new SqlDataAdapter("insert into tbl_shipping(uid,fname,lname,gender,email,city,state,country,mobileno,cdate)values(" + Convert.ToInt32(Session["uid"].ToString()) + ",'" + Session["fname"].ToString() + "','" + Session["lname"].ToString() + "','" + Session["gender"].ToString() + "','" + Session["email"].ToString() + "','" + Session["city"].ToString() + "','" + Session["state"].ToString() + "','" + Session["country"].ToString() + "','" + Session["mobileno"].ToString() + "',getdate())", con);
                        da.SelectCommand.ExecuteNonQuery();
 da = new SqlDataAdapter("insert into tbl_cardtype(uid,cardname,cdate)values(" + Convert.ToInt32(Session["uid"].ToString()) + ",'" + RadioButton1.Text + "',getdate())", con);
                        da.SelectCommand.ExecuteNonQuery();
                        string s = "your artconfirmation is successfully completed.";
                        Response.Redirect("~/user in box/productconfirmation.aspx?confirm=" + s.ToString());
 
                    }
                }
Posted
v2
Comments
Do like below...

if(Session["uid"] != null)
{
da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"].ToString()) + " ", con);
}

And check if it is going inside the if statement or not.
mayuri koul 3-Jun-13 3:57am    
i tried this but then it is showing the error in the next line :(
So, is it going inside the if statement ?

And what is the value of Session here. Can you check with debugger?
mayuri koul 3-Jun-13 4:33am    
ya its going inside the if statemnet..but the next line starts showing the same error
Ok. So what is the value of Session["uid"] when you go inside the if statement?
Please check.

looks like Session["uid"] is null check it and do do any further process if that is null
 
Share this answer
 
Comments
mayuri koul 3-Jun-13 3:41am    
please can you explain me that with an example referring to my code..please sir/maam :(
Sajith Dilhan 4-Jun-13 5:30am    
//try as below

if(Session["uid"] != null)
{
da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"].ToString()) + " ", con);
ds = new DataSet();
da.Fill(ds, "tbl_tatc");
.................
}
Hi mayuri

The value of Session["uid"]is null in your code .that is the region of error.
 
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