>>>Hi everybody,
>>>
>>>I have the following code
>>>
>>>
>>>foreach (KeyValuePair<String, String> kvp in rowValues)
>>> {
>>> String passedValue = kvp.Value.Trim();
>>> String rowValue = bookingRow[kvp.Key].ToString().Trim();
>>> if (passedValue != rowValue)
>>> {
>>> this.SaveToBookingHistory(booking_id, "M", kvp.Key, rowValue, passedValue, ref messageText, ref statusCode);
>>> }
>>> }
>>>
>>>The problem here is that, for example, we're not passing default values, say, for Integer column the passed value will be empty string and 0 in the row value. It should be treated the same, but it doesn't.
>>>
>>>I am wondering how to properly write the code above. I need to somehow figure out the type of rowValue (which comes from the DataRow) and cast passedValue into that type.
>>>
>>>Do you know what can I do here?
>>>
>>>Thanks in advance.
>>
>>The best thing you could do would be to change the rowValues to use a KeyValuePair of string, object instead. Then the types wouldn't need to be converted. Of course that may not be possible depending on where that came from. Keep in mind you may need to use the Equals method to compare objects.
>>
>>If it isn't possible to change the type of rowValues, you could get the type from the column from bookingRow.Table.Columns[kvp.Key]. Use the column's type to try to convert your kvp.Value using either Convert.ChangeType (
http://msdn.microsoft.com/en-us/library/dtb69x08%28v=vs.100%29.aspx), or writing your own conversion method to parse, cast, or convert as appopriate.
>
>Unfortunately, Convert.ChangeType can not convert empty string into 0 (as I hoped), so I am back to square one.
>
>I found this
http://stackoverflow.com/questions/1106974/how-can-i-convert-to-a-specific-type-in-a-generic-version-of-tryparse?rq=1 but it's way over my head.
>
>So, how can I adjust this code assuming that I will be passing empty strings for default values (such as 0, false, NULL for date, etc.):
>
>
> String passedValue = kvp.Value.Trim();
> var rowValue = bookingRow[kvp.Key];
> if (Convert.ChangeType(passedValue, rowValue.GetType()) != rowValue)
> {
> 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);
>
>In another post I found this method
>
>public static T SafeConvert<T>(string s, T defaultValue)
>{
> if ( string.IsNullOrEmpty(s) )
> return defaultValue;
> return (T)Convert.ChangeType(s, typeof(T));
>}
>
>However, I don't see how will it help me as I don't know what default value to pass.
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.