Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I use the DataTable Select method to get find the most recent "LastModified" date in a table of similarly named files. I then use the resulting date and time to get the specific filename and path from the same DataTable.

 

The problem is that this worked fine on my Windows XP machine, but did not work on any of several Windows 2000 computers at the client's location. The Select method would always return 0 (zero) rows even though we were working with the exact same list of files in both locations.

 

As part of some experimentation, I changed the Type of the "LastModified" from "System.DateTime" to "System.String" and everything began working as expected at the client's location.

 

If anyone has any clue as to why I cannot use the "System.DateTime" DataColumn type in the client's Windows 2000 environment when it works perfectly in my Windows XP environment; I would greatly appreciate any input.

 

Here are the relevant code snippets, starting with the DataTable definition:

 

DataTable fileinfo = [color=Blue]new[/color] DataTable("TestFileInfo");
fileinfo.Columns.Add("FileName", Type.GetType("System.String"));
fileinfo.Columns.Add("FileSize", Type.GetType("System.UInt64"));
[color=Green]/* DEBUG BEGIN: The following line does not work in client environment...
fileinfo.Columns.Add("LastModified", Type.GetType("System.DateTime"));
*/[/color]
fileinfo.Columns.Add("LastModified", Type.GetType("System.String"));
[color=Green]/* DEBUG END */[/color]
fileinfo.Columns.Add("FullPath", Type.GetType("System.String"));

And now the logic that performs the lookup using the Select method:

[color=Blue]string[/color] exprFile = "FileName LIKE '" + pattern + "*'";
[color=Blue]string[/color] latestFileDate = dtFiles.Compute("MAX(LastModified)", exprFile).ToString();

[color=Blue]string[/color] exprDate = exprFile + " AND LastModified = '" + latestFileDate + "'";

DataRow[] foundRows = dtFiles.Select(exprDate);
[color=Blue]string[/color] latestFile = null;
[color=Blue]if[/color] (foundRows.Length > 0)
latestFile = foundRows[0]["FullPath"].ToString();
[color=Blue]return[/color] latestFile;

 

The above code returns 0 (zero) rows if "LastModified" DataColumn type set to System.DateTime. Any help, suggestions, or even wild guesses would be greatly appreciated.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
  • *Experts*
Posted

I can't think of anything different on Windows 2000 from Windows XP that would affect the way your code is working. I have multiple clients including two QA machines that work fine with the same code you have (well, similar, not the same).

 

I wonder about the date format:

What does the string latestFileDate show? You're not specifying a format for the date string and so it will be up to Windows Regional settings. Maybe try forcing something like "MM/dd/yyyy hh:mm:ss" (that may not be right, going from memory as an example).

 

Can you verify this is a problem on the client machine using a small test program? When I have "client" issues, I'll write a small EXE that mimics the code in question. Something that creates the DataSet, fills it with two rows, and performs the filter you describe. Try running that on the client machine and see if it works/doesn't work. If it doesn't, you can upload that for us to try.

 

Note (nothing to do with your problem):

I'm not sure which is better, but I like to use the following when adding columns dynamically:

fileinfo.Columns.Add("LastModified", typeof(System.DateTime));

 

Using typeof can generate compile error if System.DateTime isn't spelled right, whereas using Type.GetType from a string might have runtime errors.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

I did forget to mention that I was using the pound sign (#) around my date-time as recommended in the DataColumn Expression reference.

 

I will check the regional settings and write a quick stand-alone test case and let you know what I find.

 

I can tell you that the exprDate string would always properly append the lastFileDate string, which would always match the LastModified DateTime value, even in cases where the formatting was different from computer to computer.

 

I tend to think it is not related to formatting because the DataTable.Compute method returns the exact date and time I am going to use to find the file with that exact date and time. Whatever format is returned by the Compute method is the format I use in my Expression.

 

Anyway, I will keep you all posted in this thread. Thanks for the suggestions and the help. I like the 'typeof' tip, very smart.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte
Posted

ugh!

 

I sound like a broke record, but here goes. . .

 

USE PARAMETERS!!!!

 

dates are dates not strings!!!

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

I'm not quite up to speed on all this stuff. Parameters? I use parameters in SQL statements all the time, but I'm not familiar with using them for DataColumn Expressions.

 

Further explanation would be greatly appreciated.

Never ascribe to malice that which is adequately explained by incompetence. - Napoleon Bonaparte

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...