对象引用未设置为对象的实例...在asp.net的aspx.cs页面中 [英] Object reference not set to an instance of an object...in aspx.cs page of asp.net

查看:66
本文介绍了对象引用未设置为对象的实例...在asp.net的aspx.cs页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中有一行错误

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_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());
 
                    }
                }

推荐答案

看起来像Session [uid]是null检查它并且如果为null则执行任何进一步的处理
looks like Session["uid"] is null check it and do do any further process if that is null


您好mayuri



Session [uid]的值为null在你的代码中。这是错误的区域。
Hi mayuri

The value of Session["uid"]is null in your code .that is the region of error.


这篇关于对象引用未设置为对象的实例...在asp.net的aspx.cs页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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