2 vdfvdv vdfvdv 于 2014.12.03 15:36 提问

急求!!asp.net制作网站时显示“未将对象引用设置到对象的实例。”

求大神帮忙解决;我想要实现“点击加入购物车”,然后就将datalist中显示的图书和用户添加到数据库,并在另一界面显示购物车,加入购物车的按钮的源代码是这样的:编译没错,调试就出现“未将对象引用设置到对象的实例。”菜鸟级找不到解决方法了。
protected void Button1_Click2(object sender, EventArgs e)
{
if (Session["name"] == null)
{
Response.Redirect("index.aspx");
}
else
{
string connectionString = "server=.; DataBase=BookStore; Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
try
{
foreach (DataListItem item in DataList2.Items)
{
int Bamount = int.Parse(Request.QueryString["Tnventory"].ToString());
int CBamount = int.Parse(this.camount.Text.Trim().ToString());
string username = Session["name"].ToString();
string bookid = Request.QueryString["bookid"].ToString();

                    string BookName = Request.QueryString["BookName"].ToString();
                    float Price = float.Parse(Request.QueryString["Price"].ToString());
                    int Cnum = 0;
                    string select = "select Cname from car where Cname=@UserName";
                    SqlCommand cmd = new SqlCommand(select, conn);
                    cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 10);
                    cmd.Parameters["@UserName"].Value = username;
                    SqlDataReader sdr;
                    sdr = cmd.ExecuteReader();
                    int o = 0;
                    SqlCommand cd;
                    while (sdr.Read())
                    {
                        o = 1;
                        sdr.Close();
                        string sql = "select MAX(Cnum) from car where Cname='" + username + "' ";
                        cd = new SqlCommand(sql, conn);
                        string str = cd.ExecuteScalar().ToString();
                        int h = int.Parse(str);
                        Cnum = h + 1;
                        break;
                    }
                    if (o == 0)
                    {
                        sdr.Close();
                        Cnum = 1;
                    }

                    if (Bamount < CBamount)
                    {
                        Response.Write("<script type=\"text/javascript\">alert(\"库存不足!\");</script>");
                    }
                    else
                    {
                        float Cprice = Price * CBamount;
                        string sql1 = "insert into car(Cname,Cnum,bookid,BookName,Price,CBamount,Cprice) values('" + username + "','" + bookid + "','" + BookName + "','" + Price + "','" + CBamount + "','" + Cprice + "')";
                        SqlCommand cmd1 = new SqlCommand(sql1, conn);
                        cmd1.ExecuteNonQuery();
                        Response.Write("<script type=\"text/javascript\">alert(\"已经加入购物车!\");</script>");
                    }
                }
            }

            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

    }

4个回答

sscrazy
sscrazy   2014.12.03 16:49

写出具体是哪行报错更方便查找问题答案

vdfvdv
vdfvdv 它没有显示哪行错
3 年多之前 回复
q107770540
q107770540   Ds   Rxr 2014.12.03 17:51

Request.QueryString["Tnventory"]
Request.QueryString["bookid"]
这些之类的URL参数有都传了么?

vdfvdv
vdfvdv datalist里都有显示,但是好像是从datalist中读取没有成功
3 年多之前 回复
q107770540
q107770540   Ds   Rxr 2014.12.03 17:52

在服务器上打开一下你的网站,具体的错误行号,在服务器器上是可以看到的

qq_38713711
qq_38713711   2017.05.10 11:30

string BookName = Request.QueryString["BookName"].ToString();
float Price = float.Parse(Request.QueryString["Price"].ToString());
是这一段代码没有获取到前台控件吧?怎么解决的??

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!