当前位置:编程学习 > C#/ASP.NET >>

ASP.NET相关问题,在0处没有任何行...哥哥姐姐们行行好,帮我看看,我都要哭了T T

if (!IsPostBack) { //向购物车中添加商品,如果购物车中已经存在该商品,则商品数量加1,如果是第一次购买,则向购物车中添加一条商品信息 string P_str_CartID = Session["UserID"].ToString(); string P_str_GoodsID = Request.QueryString["sp_WareID"]; DataSet ds1 = DB.reDs("select count(*) from T_ShoppingInfo where Scar_ID='" + P_str_CartID + "'and sp_WareID='" + P_str_GoodsID+"'"); if (ds1.Tables[0].Rows[0][0].ToString() == "0") { DataSet ds = DB.reDs("select sp_WareName,sp_Price from T_Ware where sp_WareID='" + P_str_GoodsID + "'"); string P_str_GoodsName = ds.Tables[0].Rows[0][1].ToString();//错误在这里:在0处没有任何行 string P_str_GoodsPrice = ds.Tables[0].Rows[0][2].ToString(); string P_str_Num = "1"; DB.ExSql("insert into T_ShoppingInfo (Scar_ID,sp_WareID,sp_WareName,sp_Price,shop_Num) values('" + P_str_CartID + "','" + P_str_GoodsID + "','" + P_str_GoodsName + "','" + P_str_GoodsPrice + "','" + P_str_Num + "')"); } else { DB.ExSql("update T_ShoppingInfo set shop_Num=shop_Num+1 where Scar_ID='" + P_str_CartID + "'and sp_WareID='" + P_str_GoodsID+"'"); } //显示购物车中的商品信息 Bind(); } } 唉,菜鸟的悲哀- - 大侠们帮帮,, 要搞个购物车都这么不容易。..泪奔。。。
追问:大哥,我觉得您说的很有道理。。。但是可不可以具体一点?我倍茫然。。那得怎么搞呢?!不太明白,有具体的代码么?if(ds.Tables[0].rows.count>0)这句 提示“System.Data.DataRowCollection”不包含“count”的定义 ,这可如何是好。。加入这两个判断后提示“System.Data.DataRowCollection”不包含“count”的定义 ,这可如何是好。。
答案:你的代码问题多多,重复多多   
     DataSet ds1 = DB.reDs("select count(*) from T_ShoppingInfo where Scar_ID='" + P_str_CartID + "'and sp_WareID='" + P_str_GoodsID+"'"); ----这里的sql语句查询数量没必要用sqldataadapter的fill,而应该executescalar这个方法,因为结果只是一行一列,而且必有值
            if (ds1.Tables[0].Rows[0][0].ToString() == "0")
            {

                DataSet ds = DB.reDs("select sp_WareName,sp_Price from T_Ware where sp_WareID='" + P_str_GoodsID + "'"); 
---这里获得一个ds,你还没判断有没有数据就开始ds。table。了,那不是直接报错么?
其他:string P_str_GoodsName = ds.Tables[0].Rows[0][1].ToString();//错误在这里:在0处没有任何行
这说明你ds为空。
也就是说你取Row[0]行超出了索引。 您好:
      前面加2个判断比较保险

     if(ds.Tables[0] != null)
            {
                 if(ds.Tables[0].Rows.count != 0)
                     {
                         //这边放你那句错误代码开始。 记得 你传过来的ID 除非是非法传过来的,不然肯定是有记录的 这边找不到 说名那个值错了 或者 找错表之类的,断点跟踪下看看!!
                     }
            } 看你数据库有没有数据或者你连接数据库是不是有问题,那个错误提示表示是你的DataSet里面没有数据 ds 数据集为空  也就是没有数据
为了防错 你可以在赋值前  先判断 是否有值   有的话继续操作   没有就提示信息好了 

上一个:.net 邮件发不出去
下一个:从网上下载了个asp.net源码,它要怎么打开

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,