0

For reasons beyond my control, I find myself using .net mobile 3.5. I need to be able to use the Rows.Find(Object) function on a DataTable where the Primary Key is a DateTime.

Ex: DataRow temp = exDataSet.exDataTable.Rows.Find(exDateTime);

I have not been able to make the C# DateTime value match that which is stored in the DataTable.

Based on guidance from these forums and from other resources I have tried changing the DataColumn's DateTimeMode, I have tried converting the DateTime to SqlDateTime, and I have tried the (much less desired) .ToString("yyyy-MM-dd HH:mm:ss") function, all to no avail. Also, I am unable to use DateTime2 as far as I am aware.

Any recommendations?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Is the problem *specific* to .net mobile 3.5? Can you reproduce the problem in a c# desktop console app, or a [.net fiddle](https://dotnetfiddle.net/)? Also, from the [docs](https://msdn.microsoft.com/en-us/library/wk5t802s(v=vs.110).aspx) for `DateTime.Equals()`: *t1 and t2 are equal if their Ticks property values are equal. TheirKind property values are not considered in the test for equality.* Maybe the `DateTime` keys in your table have some tiny difference in clock ticks that you aren't seeing when you print to string? – dbc Jan 10 '18 at 21:54
  • 1
    The problem itself is not specific to .net mobile 3.5, but the solution may be. Some features and proposed solutions elsewhere, such as DateTime2, were introduced only in later or non-mobile versions. – Jonathan Doe Jan 11 '18 at 15:08

1 Answers1

0

You need to make both Dates in same format before comparison, Just sharing an example as below , you can use same concept in your code.

DateTime dt1;
DateTime dt2;

if (DateTime.TryParse(strDate1, out dt1) && DateTime.TryParse(strDate2, out dt2))
 {
    if (dt1.Date == dt2.Date)
    {
       // the dates are identical
    }
}
Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20
  • That would work for two C# DateTime objects, but I need to be able to use the .Rows.Find(Object) function with a C# DateTime value as a parameter - this will compare the C# DateTime value against all DateTime values in the primary key column of the DataTable, which seem to use a slightly different DateTime format if I am not mistaken. – Jonathan Doe Jan 10 '18 at 21:20
  • you simple need to pass your C# DateTime in that format, I am not sure about your DataTable PK format. – Ehsan Ullah Nazir Jan 10 '18 at 21:35
  • The two DateTime format implementations appear to differ, and I am unsure exactly how they differ. My lack of understanding in regards to their differences is the real problem. – Jonathan Doe Jan 10 '18 at 21:42
  • Might be am considering this much simple ,Query over SQL table to know the format.if that helps? – Ehsan Ullah Nazir Jan 10 '18 at 21:46
  • By the way ,why are you creating DateTime as PK?? – Ehsan Ullah Nazir Jan 10 '18 at 21:47
  • Because it is the only value in the set that I can guarantee will be unique. – Jonathan Doe Jan 11 '18 at 15:03