System.NullReferenceException:对象引用未设置为object.Under lnkCheckout_Click事件的实例。 [英] System.NullReferenceException: Object reference not set to an instance of an object.Under lnkCheckout_Click event.

查看:60
本文介绍了System.NullReferenceException:对象引用未设置为object.Under lnkCheckout_Click事件的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);


            }
        }
}

推荐答案

您使用了Session变量和类型转换它没有检查空引用。



行:
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.


我猜测(从可能的行号)这是错误行?

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);

如果是这种情况,则必须在执行ToString之前检查每个值() 在上面。如果Object为null,则ToString()将为您提供此错误
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


这篇关于System.NullReferenceException:对象引用未设置为object.Under lnkCheckout_Click事件的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆