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. General Programming
  3. C#
  4. DataTable.Select() PRoblem

DataTable.Select() PRoblem

Scheduled Pinned Locked Moved C#
helpcssannouncement
14 Posts 3 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.
  • J jonhbt

    Sry for that I didn't want to cause any problems the code that I am using is this DataTable dtAllEmps = new DataTable(); dtAllEmps = cetData.GetALLEmployessByDate(conString, sensors); foreach (DataRow dataRow in dtAllEmps.Rows) { if(dtCSV.Rows.Count == 0) { DataRow newRow = dtCSV.NewRow(); newRow["UserID"] = dataRow["USERID"]; newRow["Department"] = dataRow["DEPTNAME"]; newRow["Date"] = dataRow["CHECKTIME"]; newRow["Name"] = dataRow["NAME"]; newRow["CheckType"] = dataRow["CHECKTYPE"]; dtCSV.Rows.Add(newRow); } else { Console.WriteLine("User ID " + dataRow["USERID"].ToString()); if (dataRow["USERID"].ToString() != "") { string strExpr = "UserID = " + dataRow["USERID"].ToString(); Console.WriteLine("Expression " + strExpr); DataRow[] filteredRows = dtCSV.Select(strExpr); if (filteredRows.Count() == 1) { Console.WriteLine("Number of Rows " + filteredRows.Count().ToString()); DataRow csvRow = filteredRows[0]; DateTime csvDate = Convert.ToDateTime(csvRow["Date"]); DateTime tableDate = Convert.ToDateTime(dataRow["CHECKTIME"]); Console.WriteLine("csvDate" + csvRow["Date"].ToString()); Console.WriteLine("tableDate" + dataRow["CHECKTIME"].ToString()); if (csvDate < tableDate) { filteredRows[0]["Date"] = tableDate.ToString(); filteredRows[0]["CheckType"] = dataRow["CHECKTYPE"].ToString(); filteredRows[0]["Department"] = dataRow["DEPTNAME"].ToString(); }

    M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #5

    Can you put a Try Catch in there and post the error message from the catch? Put the whole block of code from the foreach in a try block and then a catch at the end. This should give you better idea of what the error is.

    Excellence is doing ordinary things extraordinarily well.

    J 1 Reply Last reply
    0
    • J jonhbt

      I am writing the console calls so that to know what is the last value that the program is having. It is crashing in this part. The values when the program throws the exception differ. string strExpr = "UserID = " + dataRow["USERID"].ToString(); Console.WriteLine("Expression " + strExpr); DataRow[] filteredRows = dtCSV.Select(strExpr);

      T Offline
      T Offline
      Tom Deketelaere
      wrote on last edited by
      #6

      Don't worry you didn't cause problems, was just a hint. Another hint: When posting code use the 'code block' tags, makes it easier to read the code. As to your problem. At what userid does the error occur? My guess would be that since you do a 'tostring' your userid is of the type string (varchar). If so your filter expression should be:

      "UserID = '" + datarow["USERID"].ToString() + "'";

      J 1 Reply Last reply
      0
      • M MumbleB

        Can you put a Try Catch in there and post the error message from the catch? Put the whole block of code from the foreach in a try block and then a catch at the end. This should give you better idea of what the error is.

        Excellence is doing ordinary things extraordinarily well.

        J Offline
        J Offline
        jonhbt
        wrote on last edited by
        #7

        I have a try catch but, only error prompting is System.argument Exception{"Min (14) must be less than or equal to max (-1) in a Range object."}

        1 Reply Last reply
        0
        • T Tom Deketelaere

          Don't worry you didn't cause problems, was just a hint. Another hint: When posting code use the 'code block' tags, makes it easier to read the code. As to your problem. At what userid does the error occur? My guess would be that since you do a 'tostring' your userid is of the type string (varchar). If so your filter expression should be:

          "UserID = '" + datarow["USERID"].ToString() + "'";

          J Offline
          J Offline
          jonhbt
          wrote on last edited by
          #8

          thanks for the advices. Regarding to the problem the values are Numbers in the database. There wasn't only one value since i tried different values. One of them was 279 which was giving problems. the strange thing is that this process runs for most of the values but at a certain point in time it stops. that is why i am confused, since all the values that are being passed are numbers.

          T 1 Reply Last reply
          0
          • J jonhbt

            thanks for the advices. Regarding to the problem the values are Numbers in the database. There wasn't only one value since i tried different values. One of them was 279 which was giving problems. the strange thing is that this process runs for most of the values but at a certain point in time it stops. that is why i am confused, since all the values that are being passed are numbers.

            T Offline
            T Offline
            Tom Deketelaere
            wrote on last edited by
            #9

            Is your userid column in the datatable numeric? If so remove the tostring (not needed here) If not try the code I gave (but I don't think that's the problem)

            jonhbt wrote:

            the strange thing is that this process runs for most of the values but at a certain point in time it stops

            Does it always stop at the same value? or is random? Are you using multi - threading?

            J 1 Reply Last reply
            0
            • T Tom Deketelaere

              Is your userid column in the datatable numeric? If so remove the tostring (not needed here) If not try the code I gave (but I don't think that's the problem)

              jonhbt wrote:

              the strange thing is that this process runs for most of the values but at a certain point in time it stops

              Does it always stop at the same value? or is random? Are you using multi - threading?

              J Offline
              J Offline
              jonhbt
              wrote on last edited by
              #10

              no im not useing multi threading. The value is the same only for the set of values chosen. WHen i change the set of values by a filter from the select command in the database, the value is different.

              T 1 Reply Last reply
              0
              • J jonhbt

                no im not useing multi threading. The value is the same only for the set of values chosen. WHen i change the set of values by a filter from the select command in the database, the value is different.

                T Offline
                T Offline
                Tom Deketelaere
                wrote on last edited by
                #11

                When you debug and stop at the select line try doing a 'quick watch' (right mouse click) Then run the select (dtscv.select("Userid=" + newrow["userid"]) ). Does that work? Is the value present or not? Can you post your declaration of the dtscv datatable? (specifically the column declarations )

                J 1 Reply Last reply
                0
                • T Tom Deketelaere

                  When you debug and stop at the select line try doing a 'quick watch' (right mouse click) Then run the select (dtscv.select("Userid=" + newrow["userid"]) ). Does that work? Is the value present or not? Can you post your declaration of the dtscv datatable? (specifically the column declarations )

                  J Offline
                  J Offline
                  jonhbt
                  wrote on last edited by
                  #12

                  This is my declaration of te DataTable

                  DataColumn Column1 = new DataColumn("Name");
                  DataColumn Column2 = new DataColumn("Department");
                  DataColumn Column3 = new DataColumn("Date");
                  DataColumn Column4 = new DataColumn("UserID");
                  DataColumn Column5 = new DataColumn("CheckType");

                              dtCSV.Columns.Add(Column1);
                              dtCSV.Columns.Add(Column2);
                              dtCSV.Columns.Add(Column3);
                              dtCSV.Columns.Add(Column4);
                              dtCSV.Columns.Add(Column5);
                  

                  Ill try what you suggested

                  T 1 Reply Last reply
                  0
                  • J jonhbt

                    This is my declaration of te DataTable

                    DataColumn Column1 = new DataColumn("Name");
                    DataColumn Column2 = new DataColumn("Department");
                    DataColumn Column3 = new DataColumn("Date");
                    DataColumn Column4 = new DataColumn("UserID");
                    DataColumn Column5 = new DataColumn("CheckType");

                                dtCSV.Columns.Add(Column1);
                                dtCSV.Columns.Add(Column2);
                                dtCSV.Columns.Add(Column3);
                                dtCSV.Columns.Add(Column4);
                                dtCSV.Columns.Add(Column5);
                    

                    Ill try what you suggested

                    T Offline
                    T Offline
                    Tom Deketelaere
                    wrote on last edited by
                    #13

                    try:

                    DataColumn Column4 = new DataColumn("UserID",typeof(int));

                    J 1 Reply Last reply
                    0
                    • T Tom Deketelaere

                      try:

                      DataColumn Column4 = new DataColumn("UserID",typeof(int));

                      J Offline
                      J Offline
                      jonhbt
                      wrote on last edited by
                      #14

                      I think that solved the problem. I stil have to takle the performance issue, but THANKS alot for your help

                      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