Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing Data Row column values and string values
Message
From
10/07/2013 11:39:45
 
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:
01578108
Views:
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.


Like this ? (untested)
>
>foreach (KeyValuePair<String, String> kvp in rowValues)
>         {
>            String passedValue = kvp.Value.Trim();          //  I would not Trim() here.  Make sure the are Trimmed before they go in the dictionary
>            string rowValue = bookingRow[kvp.Key].ToString().Trim();

>            if ( passedValue != rowValue ) 
>            {
>   >                  this.SaveToBookingHistory(booking_id, "M", kvp.Key, rowValue , passedValue, ref messageText, ref statusCode);
>            }
>         }
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform