这个不算太好,写个方法作try catch 判断返回值

解决方案 »

  1.   

    int i=0;
            for (i=0;i<dtb.Rows.Count;i++)
            {            if (dtb.Rows(i)("userId")==userId)  //此行出错!
                {
                    Response.Write("已经存在");
                }
                else
                {
                    Response.Write("bu存在");
                }
            }
    错误 1 “System.Data.DataTable.Rows”是“属性”,但此处被当做“方法”来使用
      

  2.   

    我觉得用try catch不好。
    用正则表达式应该会快些的
      

  3.   

    我刚学.net .能帮我把 你们认为比较好的解决方法和代码一起发给我。谢谢!
      

  4.   

    for (i=0;i<dtb.Rows.Count;i++)
    {if (dtb.Rows[i]["userId"]==userId) //此行出错!
    {
    Response.Write("已经存在");
    }
    else
    {
    Response.Write("bu存在");
    }
    }
      

  5.   

    Convert.toint32(dtb.rows[i]["userID"])==userID
      

  6.   

    有一个简单的方法DataRows[] drs = dtb.Select("userID="+userId);
    if(drs.Length >0)
    {
       //有此 userId
    }
      

  7.   

    for (i=0;i<dtb.Rows.Count;i++)
    {if (dtb.Rows[i]["userId"]==userId+"") //此行出错!
    {
    Response.Write("已经存在");
    }
    else
    {
    Response.Write("bu存在");
    }
    }
    加了个+"" 就不用toint转换了。
      

  8.   

    dtb.Rows[i]["userId"]这是object类型的。。
      

  9.   


    DataRows[] drs = dtb.Select("userID="+userId.ToString());//忘记看类型了
    //DataRows[] drs = dtb.Select("userID="+userId.ToString()+" and (userSex ='"+userSex +"'" +" or userName='"+userName+"')");//举一反三
    if(drs.Length >0)
    {
       //有此 userId
    }
      

  10.   

    谢谢大家,现在我做了个数量,让程序可在回发(模拟对已存在货品购物时)时加1  ,可两个控件都总是显示数量是1 ,怎么也不加。大家再帮我是什么原因?protected void Page_Load(object sender, EventArgs e)
        {
           
            if (IsPostBack)
            {
                int userId = Convert.ToInt32(this.DetailsView1.Rows[0].Cells[1].Text);
                string userName = this.DetailsView1.Rows[1].Cells[1].Text.ToString();
                string userSex = this.DetailsView1.Rows[4].Cells[1].Text.ToString();
                           //DataSet ds = new DataSet();
                DataTable dtb = new DataTable("MyCar");
                dtb.Columns.Add("userId", typeof(int));
                dtb.Columns.Add("userName");
                dtb.Columns.Add("userSex");
                dtb.Columns.Add("TheCount");            int a = 1;
                DataRow dRow = dtb.NewRow();
                dRow["userId"] = userId;
                dRow["userName"] = userName;
                dRow["userSex"] = userSex;
                dRow["TheCount"] = a;
                DataRow[] drs = dtb.Select("userID=" + userId);
                if (drs.Length > 0)
                {
                    a+=1;
                }
                else
                {
                    a = 1;
                }            dtb.Rows.Add(dRow);            Session["myCar"] = dtb;            this.DetailsView2.DataSource = (DataTable)Session["myCar"];
                this.DetailsView2.DataBind();            this.Gv2.DataSource = dtb.DefaultView;
                this.Gv2.DataBind();        }
      

  11.   

    protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtb = Session["myCar"] as DataTable;
            if(dtb==null)
    {        
                //DataSet ds = new DataSet();
                DataTable dtb = new DataTable("MyCar");
                dtb.Columns.Add("userId", typeof(int));
                dtb.Columns.Add("userName");
                dtb.Columns.Add("userSex");
                dtb.Columns.Add("TheCount");
    }
            if (IsPostBack)
            {
                int userId = Convert.ToInt32(this.DetailsView1.Rows[0].Cells[1].Text);
                string userName = this.DetailsView1.Rows[1].Cells[1].Text.ToString();
                string userSex = this.DetailsView1.Rows[4].Cells[1].Text.ToString();
               
                int a = 1;
                DataRow dRow = dtb.NewRow();
                dRow["userId"] = userId;
                dRow["userName"] = userName;
                dRow["userSex"] = userSex;
                //dRow["TheCount"] = a;            DataRow[] drs = dtb.Select("userID=" + userId);
                if (drs.Length > 0)
                {
                    a+=1;
                }
                else
                {
                    a = 1;
                }
                dRow["TheCount"] = a; //在这赋值
                dtb.Rows.Add(dRow);            Session["myCar"] = dtb;            this.DetailsView2.DataSource = (DataTable)Session["myCar"];
                this.DetailsView2.DataBind();            this.Gv2.DataSource = dtb.DefaultView;
                this.Gv2.DataBind();        }
      

  12.   

    dataview dv=datatable.default;
    dv.Rowfile = "想要过滤信息";
      

  13.   

    可能我的这段代码还有逻辑问问题,请大家再帮我看看. protected void BtnAddToCar_Click(object sender, EventArgs e)
        {        int goodsId = Int32.Parse(this.DetailsView1.Rows[0].Cells[1].Text);
            string theGoodsName = (this.DetailsView1.Rows[1].Cells[1].Text).ToString();
            decimal thePrice = Convert.ToDecimal(this.DetailsView1.Rows[5].Cells[1].Text);        //DataTable dtb = Session["myCar"] as DataTable;
            // DataTable dtb = new DataTable("MyCar");
                   if (Session["myCar"] == null)
            {
                //DataSet ds = new DataSet();
                DataTable dtb = new DataTable("MyCar");
                dtb.Columns.Add("goodsId", typeof(int));
                dtb.Columns.Add("thtGoodsName");
                dtb.Columns.Add("thtPrice");
                dtb.Columns.Add("TheCount");            DataRow dRow = dtb.NewRow();
                dRow["goodsId"] = goodsId;
                dRow["thtGoodsName"] = theGoodsName;
                dRow["thtPrice"] = thePrice;
                dRow["TheCount"] = 1;
                dtb.Rows.Add(dRow);            Session["myCar"] = dtb;
            }        else
            {
                DataTable dtb = Session["myCar"] as DataTable;
                //dtb.Columns.Add("goodsId", typeof(int));
                int a = 1;
                DataRow[] drs = dtb.Select("goodsId=" + goodsId);
                if (drs.Length > 0)
                {
                    a += 1;
                    //怎样将 a 的值写入 DataTable dtb 而不需要添加新行!
                    //DataRow[] drs = dtb.Rows[goodsId];
                }            else
                {
                    a = 1;
                    DataRow dRow = dtb.NewRow();
                    dRow["goodsId"] = goodsId;
                    dRow["thtGoodsName"] = theGoodsName;
                    dRow["thtPrice"] = thePrice;
                    dRow["TheCount"] = a;
                    dtb.Rows.Add(dRow);
                }            Session["myCar"] = dtb;
                  this.DetailsView2.DataSource = dtb;
                this.DetailsView2.DataBind();
            }
      }
      

  14.   


                if (drs.Length > 0)
                {
                    a += 1;
                    //怎样将 a 的值写入 DataTable dtb 而不需要添加新行!
                    drs[0]["TheCount"] = a;
                }
      

  15.   

    if(dt.rows.count>0)
    {}
    else
    {}