Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. datatable filter expression......

datatable filter expression......

Scheduled Pinned Locked Moved ASP.NET
databasesysadminquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RajpootRohan
    wrote on last edited by
    #1

    Hi to all, I have in a datatable select method. I am unable to find what is going wrong. Please give some guidance. I am trying to do like this:

    public void get\_data()
    {
        try
        {
            pro\_id = Request.QueryString\["pr\_id"\];
            quanti = Convert.ToInt32(Request.QueryString\["quant"\]);
    
            if (pro\_id != null)
            {
                SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted\_Connection=yes");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "select ICEMACHINES.product\_id,ICEMACHINES.sh\_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product\_id LIKE @product\_id union select GLASSWARE.product\_id,GLASSWARE.sh\_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product\_id LIKE @product\_id ";
                cmd.Parameters.Add("@product\_id", SqlDbType.NVarChar, 50).Value = pro\_id;
                cmd.Connection.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                ArrayList arRole1 = new ArrayList();
    
                while (rdr.Read())
                {
                    desc = (rdr\["sh\_desc"\]).ToString();
                    id = (rdr\["product\_id"\]).ToString();
                    unitprice = Convert.ToSingle((rdr\["price"\]));
                    cost = unitprice \* quanti;
    
                }
                cmd.Connection.Close();
    
                DataRow myrow = dt.NewRow();
                myrow\["REF"\] = id;
                myrow\["Description"\] = desc;
                myrow\["QTY"\] = quanti;
                myrow\["Price"\] = unitprice;
                myrow\["Cost"\] = cost;
    
                 int dtcount = dt.Rows.Count;
                 if (dtcount > 0)
                {
                DataRow\[\] arrMatchingValues = dt.Select("REF=" + id);
                if (arrMatchingValues.Length > 0)
                {
                    DataRow drUpdateMe = arrMatchingValues\[0\];
                    int drUpdateQuant = Convert.ToInt32(drUpdateMe\["QTY"\]);
                    int quan = quanti + drUpdateQuant;
                    drUpdateMe\["QTY"\] = quan;
                    dt.AcceptChanges();
                }
                }
                else
                {
                    dt.Rows.Add(myrow);
                    dt.AcceptChanges();
                }
    
                Session\["data"\] = dt;
                GridView1.DataSource = dt;
                GridView1.
    
    V R 2 Replies Last reply
    0
    • R RajpootRohan

      Hi to all, I have in a datatable select method. I am unable to find what is going wrong. Please give some guidance. I am trying to do like this:

      public void get\_data()
      {
          try
          {
              pro\_id = Request.QueryString\["pr\_id"\];
              quanti = Convert.ToInt32(Request.QueryString\["quant"\]);
      
              if (pro\_id != null)
              {
                  SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted\_Connection=yes");
                  SqlCommand cmd = new SqlCommand();
                  cmd.Connection = con;
                  cmd.CommandText = "select ICEMACHINES.product\_id,ICEMACHINES.sh\_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product\_id LIKE @product\_id union select GLASSWARE.product\_id,GLASSWARE.sh\_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product\_id LIKE @product\_id ";
                  cmd.Parameters.Add("@product\_id", SqlDbType.NVarChar, 50).Value = pro\_id;
                  cmd.Connection.Open();
                  SqlDataReader rdr = cmd.ExecuteReader();
                  ArrayList arRole1 = new ArrayList();
      
                  while (rdr.Read())
                  {
                      desc = (rdr\["sh\_desc"\]).ToString();
                      id = (rdr\["product\_id"\]).ToString();
                      unitprice = Convert.ToSingle((rdr\["price"\]));
                      cost = unitprice \* quanti;
      
                  }
                  cmd.Connection.Close();
      
                  DataRow myrow = dt.NewRow();
                  myrow\["REF"\] = id;
                  myrow\["Description"\] = desc;
                  myrow\["QTY"\] = quanti;
                  myrow\["Price"\] = unitprice;
                  myrow\["Cost"\] = cost;
      
                   int dtcount = dt.Rows.Count;
                   if (dtcount > 0)
                  {
                  DataRow\[\] arrMatchingValues = dt.Select("REF=" + id);
                  if (arrMatchingValues.Length > 0)
                  {
                      DataRow drUpdateMe = arrMatchingValues\[0\];
                      int drUpdateQuant = Convert.ToInt32(drUpdateMe\["QTY"\]);
                      int quan = quanti + drUpdateQuant;
                      drUpdateMe\["QTY"\] = quan;
                      dt.AcceptChanges();
                  }
                  }
                  else
                  {
                      dt.Rows.Add(myrow);
                      dt.AcceptChanges();
                  }
      
                  Session\["data"\] = dt;
                  GridView1.DataSource = dt;
                  GridView1.
      
      V Offline
      V Offline
      venu656
      wrote on last edited by
      #2

      hi. in this u will get only single row.?

      R 1 Reply Last reply
      0
      • R RajpootRohan

        Hi to all, I have in a datatable select method. I am unable to find what is going wrong. Please give some guidance. I am trying to do like this:

        public void get\_data()
        {
            try
            {
                pro\_id = Request.QueryString\["pr\_id"\];
                quanti = Convert.ToInt32(Request.QueryString\["quant"\]);
        
                if (pro\_id != null)
                {
                    SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted\_Connection=yes");
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "select ICEMACHINES.product\_id,ICEMACHINES.sh\_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product\_id LIKE @product\_id union select GLASSWARE.product\_id,GLASSWARE.sh\_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product\_id LIKE @product\_id ";
                    cmd.Parameters.Add("@product\_id", SqlDbType.NVarChar, 50).Value = pro\_id;
                    cmd.Connection.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    ArrayList arRole1 = new ArrayList();
        
                    while (rdr.Read())
                    {
                        desc = (rdr\["sh\_desc"\]).ToString();
                        id = (rdr\["product\_id"\]).ToString();
                        unitprice = Convert.ToSingle((rdr\["price"\]));
                        cost = unitprice \* quanti;
        
                    }
                    cmd.Connection.Close();
        
                    DataRow myrow = dt.NewRow();
                    myrow\["REF"\] = id;
                    myrow\["Description"\] = desc;
                    myrow\["QTY"\] = quanti;
                    myrow\["Price"\] = unitprice;
                    myrow\["Cost"\] = cost;
        
                     int dtcount = dt.Rows.Count;
                     if (dtcount > 0)
                    {
                    DataRow\[\] arrMatchingValues = dt.Select("REF=" + id);
                    if (arrMatchingValues.Length > 0)
                    {
                        DataRow drUpdateMe = arrMatchingValues\[0\];
                        int drUpdateQuant = Convert.ToInt32(drUpdateMe\["QTY"\]);
                        int quan = quanti + drUpdateQuant;
                        drUpdateMe\["QTY"\] = quan;
                        dt.AcceptChanges();
                    }
                    }
                    else
                    {
                        dt.Rows.Add(myrow);
                        dt.AcceptChanges();
                    }
        
                    Session\["data"\] = dt;
                    GridView1.DataSource = dt;
                    GridView1.
        
        R Offline
        R Offline
        rajmca g
        wrote on last edited by
        #3

        hi, Actually what is your task, DataRow myrow = dt.NewRow(); myrow["REF"] = id; myrow["Description"] = desc; myrow["QTY"] = quanti; myrow["Price"] = unitprice; myrow["Cost"] = cost; int dtcount = dt.Rows.Count; if (dtcount > 0) { DataRow[] arrMatchingValues = dt.Select("REF=" + id); in this code your searching for the id which is added to same datatable previously...

        R 1 Reply Last reply
        0
        • R rajmca g

          hi, Actually what is your task, DataRow myrow = dt.NewRow(); myrow["REF"] = id; myrow["Description"] = desc; myrow["QTY"] = quanti; myrow["Price"] = unitprice; myrow["Cost"] = cost; int dtcount = dt.Rows.Count; if (dtcount > 0) { DataRow[] arrMatchingValues = dt.Select("REF=" + id); in this code your searching for the id which is added to same datatable previously...

          R Offline
          R Offline
          rajmca g
          wrote on last edited by
          #4

          if your getting any error after debug plz.. write that error.. in this you will get only one row ......

          R 1 Reply Last reply
          0
          • V venu656

            hi. in this u will get only single row.?

            R Offline
            R Offline
            RajpootRohan
            wrote on last edited by
            #5

            hi, I mean to say that suppose user add a item whose product_id is "CB920". Now user again enters the same item then only the quantity should be updated. This code will only work if there will be atleast one row already in the cart.Then when the next item will come this function will check the datatable for the particular row. If found then it will update the quantity otherwise it will add a new row. This I want to do.

            cheers, sneha

            1 Reply Last reply
            0
            • R rajmca g

              if your getting any error after debug plz.. write that error.. in this you will get only one row ......

              R Offline
              R Offline
              RajpootRohan
              wrote on last edited by
              #6

              hi, Thanks for your response. Actually it is giving the error in the filter expression term. The error is "There is no column named [CB920]" This is id of the product which user is selecting.

              cheers, sneha

              R 1 Reply Last reply
              0
              • R RajpootRohan

                hi, Thanks for your response. Actually it is giving the error in the filter expression term. The error is "There is no column named [CB920]" This is id of the product which user is selecting.

                cheers, sneha

                R Offline
                R Offline
                rajmca g
                wrote on last edited by
                #7

                in this first you have to create column for dt. ex:dt.column.add[...]; after that write this code. DataRow myrow = dt.NewRow(); myrow["REF"] = id; myrow["Description"] = desc; myrow["QTY"] = quanti; myrow["Price"] = unitprice; myrow["Cost"] = cost;

                R 1 Reply Last reply
                0
                • R rajmca g

                  in this first you have to create column for dt. ex:dt.column.add[...]; after that write this code. DataRow myrow = dt.NewRow(); myrow["REF"] = id; myrow["Description"] = desc; myrow["QTY"] = quanti; myrow["Price"] = unitprice; myrow["Cost"] = cost;

                  R Offline
                  R Offline
                  RajpootRohan
                  wrote on last edited by
                  #8

                  rajmca.g wrote:

                  in this first you have to create column for dt. ex:dt.column.add[...];

                  Hi, But I have five fixed columns(REF,Description,QTY,Price,Cost).And row matching is done as per "REF" column which is already there. And at the 0th position of the datatable the column name is "REF" Please tell me which you are talking about.

                  cheers, sneha

                  U 1 Reply Last reply
                  0
                  • R RajpootRohan

                    rajmca.g wrote:

                    in this first you have to create column for dt. ex:dt.column.add[...];

                    Hi, But I have five fixed columns(REF,Description,QTY,Price,Cost).And row matching is done as per "REF" column which is already there. And at the 0th position of the datatable the column name is "REF" Please tell me which you are talking about.

                    cheers, sneha

                    U Offline
                    U Offline
                    User 2821142
                    wrote on last edited by
                    #9

                    Hi Sneha, Any way you are binding to the data to Gridview. so, you can do updating the data in gridview. then it will works fine. Actually, for data table you can't set datakeys value as we can do for gridview. cehck it out. Regards Ravi

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups