DataTable.Select() PRoblem
-
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(); }
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.
-
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);
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() + "'";
-
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.
-
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() + "'";
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.
-
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.
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?
-
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?
-
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.
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 )
-
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 )
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
-
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
try:
DataColumn Column4 = new DataColumn("UserID",typeof(int));
-
try:
DataColumn Column4 = new DataColumn("UserID",typeof(int));