Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public partial class Shipment : System.Web.UI.Page
    {
        MembershipUser mu;
        SiteMaster ms = new SiteMaster();
        Data obj = new Data();
        public string userEmail = "";
        public string adr = "";
        DataTable cartdetail = new DataTable();
        //string tempcartid = "";

        protected void Page_Load(object sender, EventArgs e)
        {
           
            try
            {
                if (User.Identity.IsAuthenticated)
                {
                    if (!Page.IsPostBack)
                    {
                        try
                        {
                            if (Session["tempcartid"].ToString() != null)
                            {

                                BindGridview(Session["tempcartid"].ToString());
                                //tempcartid = Session["tempcartid"].ToString();
                                Session["tempcartid"] = Session["tempcartid"];
                                
                            }
                            BindAddressList();
                        }
                        catch (Exception ex)
                        {

                        }

                        if (Session["user"].ToString() != null)
                        {
                            Session["user"] = Session["user"].ToString();
                            mu = Membership.GetUser(Session["user"].ToString());
                            userEmail = mu.Email.ToString();
                           // address(Session["user"].ToString());
                        }

                        else
                            Response.Redirect("~/Login.aspx", false);

                        if (Session["tempcartid"].ToString() != null)
                        {

                            Session["tempcartid"] = Session["tempcartid"].ToString();
                        }
                    }

                }
                else
                {
                    Response.Redirect("~/Login.aspx", false);

                }
            }

            catch (Exception ex)
            {

            }

            try
            {

                if (Session["tempcartid"].ToString() != null)
                {
                    DataTable itemscount = new DataTable();
                    itemscount = obj.carttotalprice(Session["tempcartid"].ToString());
                    if (itemscount.Rows.Count > 0)
                    {
                        lbl_subtotal.Text = itemscount.Rows[0]["total"].ToString();


                        lbl_total.Text = lbl_subtotal.Text;
                    }
                    else
                    {
                        lbl_subtotal.Text = "0.00";
                        lbl_total.Text = lbl_subtotal.Text;
                    }
                }
                else
                {
                    lbl_subtotal.Text = "0.00";
                    lbl_total.Text = lbl_subtotal.Text;
                }

                if (Session["tempcartid"].ToString() != null)
                {

                    Session["tempcartid"] = Session["tempcartid"].ToString();
                }

            }
            catch (Exception ex)
            {

            }

        }


        protected void lnkCheckout_Click(object sender, EventArgs e)
        {
           
            try
            {
                    if (Session["tempcartid"].ToString() != null)
                    {
                        cartdetail = obj.web_Getcartdetail(Session["tempcartid"].ToString());
                        if (cartdetail.Rows.Count > 0)
                        {
                            Recvorder(Convert.ToInt32(cartdetail.Rows[0]["CompID"].ToString()), Session["user"].ToString(), DateTime.Now.ToString(), cartdetail.Rows[0]["Comments"].ToString(), "0", "25", "0", cartdetail.Rows[0]["total"].ToString(), "Cash on Home Delivery", "", "", lbl_SelectedAddress.Text);
                        }
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Your Cart is Empty');", true);
                    }
               
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Error Occured Please contact Support!!');" + ex.ToString(), true);
            }
           
        }

        public void Recvorder(int CompID, string Customer_ID, string ordertime, string Recvorder_comments, string Recvorder_Totaldiscount, string Recvorder_Servicecharges, string Recvorder_promotioncode, string Recvorder_Totalamount, string Recvorder_Paymentmethod, string Recvorder_Paymentrecvstatus, string Recvorder_VoucherNo, string Recvorder_ShipmentAddress)
        {

           
            DataTable dtorderid = new DataTable();
            Random rand = new Random();
            string invoice = "web" + rand.Next(10000).ToString();
            cartdetail = obj.web_Getcartdetail(Session["tempcartid"].ToString());
            try
            {
                DateTime orderdatetime = DateTime.Parse(ordertime);
                
                obj.web_Recvorder(orderdatetime, CompID, Customer_ID, "Open", orderdatetime, Recvorder_comments, "", Recvorder_Totaldiscount, Recvorder_Servicecharges, Recvorder_promotioncode, Recvorder_Totalamount, Recvorder_Paymentmethod, "Not-Received", Recvorder_VoucherNo, "web", "both", 0, invoice, Recvorder_ShipmentAddress);

                
                foreach (DataRow dr in cartdetail.Rows)
                {
                   
                    obj.web_Recvorderdetail(invoice, Convert.ToInt32(dr["product_id"].ToString()), Convert.ToInt32(dr["quantity"].ToString()), float.Parse(dr["price"].ToString()), dr["SubMenuDetail"].ToString(), dr["Comments"].ToString());
                }
                //Session["tempcartid"] = "0";
                Response.Redirect("order_done.aspx", false);
               

            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Error Occured Please contact Support!!');", true);


            }
        }
}
Posted
Updated 21-Jun-15 22:04pm
v3
Comments
Andy Lanng 22-Jun-15 3:59am    
First off: Some tips on how to get an answer on these threads.

1: No one likes code dumps. You need to explain your code, I.E. which is line 130? Have you debugged? Does this happen every time? Give us more details please
2: Use the tags to format you code. I have updated this for you but you can use the buttons above the text area to format code (the yellow 'code' button)

If your post is badly formatted then people won't want to read it.

You used Session variable and typecaste it without checking null reference.

Line:
if (Session["tempcartid"].ToString() != null)


Should be
if (Session["tempcartid"] !=null && !String.IsNullOrEmpty(Convert.ToString(Session["tempcartid"])))


i.e. check for null reference before converting it to string.
 
Share this answer
 
I'm guessing (from probable line number) that this is the error line?
Recvorder(Convert.ToInt32(cartdetail.Rows[0]["CompID"].ToString()), Session["user"].ToString(), DateTime.Now.ToString(), cartdetail.Rows[0]["Comments"].ToString(), "0", "25", "0", cartdetail.Rows[0]["total"].ToString(), "Cash on Home Delivery", "", "", lbl_SelectedAddress.Text);
If that is the case then you will have to check each value before you perform ToString() on it. If the Object is null then ToString() will give you this 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