Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing Data Row column values and string values
Message
 
 
To
09/07/2013 17:52:19
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01578028
Message ID:
01578031
Views:
40
>>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.

This is how I re-wrote the code above and will be testing in a minute:
foreach (KeyValuePair<String, String> kvp in rowValues)
         {
            String passedValue = kvp.Value.Trim();
            var rowValue = bookingRow[kvp.Key];
            if (Convert.ChangeType(passedValue, rowValue.GetType()) != rowValue)
            {
               this.SaveToBookingHistory(booking_id, "M", kvp.Key, rowValue.ToString().Trim(), passedValue, ref messageText, ref statusCode);
            }
         }
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform