Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in the following code there is an error in line da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"].ToString()) + " ", con);
the error shown is-"System.NullReferenceException: Object reference not set to an instance of an object."...please help me sove this error

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_soldproducts(uid,pitemdid,productname,itemfor,brandname,imgpath,cost,cdate)values(" + Convert.ToInt32(Session["uid"].ToString()) + "," + Convert.ToInt32(Session["pitemdid"].ToString()) + ",'" + Session["productname"].ToString() + "','" + Session["itemfor"].ToString() + "','" + Session["bname"].ToString() + "','" + Session["imagepath"].ToString() + "'," + Convert.ToDecimal(Session["cost"].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());

                    }
                }

                // 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_soldproducts(uid,pitemdid,productname,itemfor,brandname,imgpath,cost,cdate)values(" + Convert.ToInt32(Session["uid"].ToString()) + "," + Convert.ToInt32(Session["pitemdid"].ToString()) + ",'" + Session["productname"].ToString() + "','" + Session["itemfor"].ToString() + "','" + Session["bname"].ToString() + "','" + Session["imagepath"].ToString() + "'," + Convert.ToDecimal(Session["cost"].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("~/UserInBox/Productconfirmation.aspx?confirm=" + s.ToString());
                //}
            }
        }
Posted
Updated 2-Jun-13 21:12pm
v2

Try adding this namespace in your page:
C#
using System.Collections;


[Edit]As question changed[/Edit]

Before using any session variable you must check for it's existence. If that session is NULL then definitely it'll show Null Reference Exception. Try this:
C#
if(Session["uid"] != null)
{
    da = new SqlDataAdapter("select cartid from tbl_addtocart where uid=" + Convert.ToInt32(Session["uid"]) + " ", con);
}



--Amit
 
Share this answer
 
v2
Comments
mayuri koul 3-Jun-13 2:57am    
oo...thanks a lot :)
_Amy 3-Jun-13 3:01am    
Welcome. :)
mayuri koul 3-Jun-13 3:13am    
can you please help me solve a new error in this code..i have edited this post
_Amy 3-Jun-13 3:22am    
Why downvoted? The question which you asked, I answered it, right? And also this was the accepted answer, why Undo? This should not happen.
Anyway, let me read your question again. I'll try to help you out, if possible.
--Amit
_Amy 3-Jun-13 3:28am    
Please see my updated answer.
Check your Session["uid"].ToString() is having the value, if session is null or blank, then you can not converted with integer value.
 
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