Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Comparing Data Row column values and string values
Message
 
 
À
10/07/2013 15:26:43
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01578028
Message ID:
01578140
Vues:
56
>>>>>>Add a second method:
>>>>>>
public static T SafeConvert<T>(string s)
>>>>>>{
>>>>>>	return SafeConvert(s, default(T));
>>>>>>}
>>>>>>
>>>>>>Also check in the debugger the type you are getting for rowValue. I believe that when you access the field like that the value is boxed as an object, meaning you are just converting to an object to compare. And since objects are reference types, you are probably comparing the address in memory to see if the reference is the same. You may need to go to the column to get the underlying type.
>>>>>
>>>>>Hi Rob and everyone,
>>>>>
>>>>>I think I may have approached this problem incorrectly from the beginning as I am not yet getting closer to the solution. I'll explain it again as it stands now and may be someone can suggest a working approach.
>>>>>
>>>>>I have a Dictionary String, String where key represent a column name and a string represent its value. All default values are empty strings, so, for example, default 0 for numeric columns will be empty string, default for char columns will be empty string, default for date (should be NULL in the table) will be empty string and finally false Boolean, I think, will also be empty string (I'll double check this).
>>>>>
>>>>>I also have DataRow bookingRow.
>>>>>
>>>>>So, this is my latest attempt (from yesterday):
>>>>>
>>>>>
>>>>>foreach (KeyValuePair<String, String> kvp in rowValues)
>>>>>         {
>>>>>            String passedValue = kvp.Value.Trim();
>>>>>            var rowValue = bookingRow[kvp.Key];
>>>>>            Type columnType = ((DataColumn)bookingRow[kvp.Key]).DataType;
>>>>>
>>>>>            var passedObject = Convert.ChangeType(passedValue, columnType);
>>>>>            if (passedObject.Equals(rowValue) == false)
>>>>>            {
>>>>>               String rowValueString = rowValue.ToString().Trim();
>>>>>               if (rowValueString!=passedValue) // Double check to prevent cases of "" vs. "          "
>>>>>                  this.SaveToBookingHistory(booking_id, "M", kvp.Key, rowValueString, passedValue, ref messageText, ref statusCode);
>>>>>            }
>>>>>         }
>>>>>
>>>>>This is not going to work as I can not Convert empty string into 0 so I'll get an exception.
>>>>>
>>>>>Do you see what can I do here and how to solve this problem?
>>>>>
>>>>>Thanks in advance.
>>>>
>>>>I also have these two new static methods:
>>>>
>>>>
>>>>/// <summary>
>>>>      /// Safe convert into desired type 
>>>>      /// http://stackoverflow.com/questions/1106974/how-can-i-convert-to-a-specific-type-in-a-generic-version-of-tryparse?rq=1
>>>>      /// </summary>
>>>>      /// <typeparam name="T"></typeparam>
>>>>      /// <param name="s"></param>
>>>>      /// <param name="defaultValue"></param>
>>>>      /// <returns></returns>
>>>>      public static T SafeConvert<T>(String s, T defaultValue)
>>>>      {
>>>>         if (String.IsNullOrEmpty(s))
>>>>            return defaultValue;
>>>>         return (T)Convert.ChangeType(s, typeof(T));
>>>>      }
>>>>
>>>>      public static T SafeConvert<T>(String s)
>>>>      {
>>>>         return SafeConvert(s, default(T));
>>>>      }
>>>>
>>>>but I don't see how can I apply them for my case.
>>>
>>>Try this (untested)
>>>
foreach (KeyValuePair<String, String> kvp in rowValues)
>>>         {
>>>            String passedValue = kvp.Value.Trim();
>>>            var rowValue = bookingRow[kvp.Key];
>>>            Type columnType = ((DataColumn)bookingRow[kvp.Key]).DataType;
>>>
>>>            var passedObject = Convert.ChangeType(passedValue, columnType);
>>>            if (!StringEquals(passedValue, rowValue, columnType))
>>>            {
>>>                  this.SaveToBookingHistory(booking_id, "M", kvp.Key, rowValueString, passedValue, ref messageText, ref statusCode);
>>>            }
>>>         }
>>>public static bool StringEquals(string stringValue, object objectValue, Type objectType)
>>>{
>>>        object convertedStringValue = GetDefault(objectType);
>>>        if (!string.IsNullOrWhitespace(stringValue))
>>>                convertedStringValue = Convert.ChangeType(stringValue, objectType);
>>>
>>>        return object.Equals(convertedStringValue, objectValue);
>>>}
>>>
>>>// See http://stackoverflow.com/questions/325426/programmatic-equivalent-of-defaulttype
>>>public static object GetDefault(Type type)
>>>{
>>>   if(type.IsValueType)
>>>   {
>>>      return Activator.CreateInstance(type);
>>>   }
>>>   return null;
>>>}
>>>
>>
>>Rob,
>>
>>Thanks a lot, but in the above this line is going to fail:
>>
>>
>> var passedObject = Convert.ChangeType(passedValue, columnType);
>>
>>
>>If the passed value is empty string and I need to convert to integer, it returns an exception.
>
>I copied and pasted your original function and it looks like I forgot to remove that line. It shouldn't be necessary.

I see now, let me try that. Thanks again.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform