Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Comparing Data Row column values and string values
Message
 
 
À
09/07/2013 19:11:23
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:
01578106
Vues:
52
>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.
If it's not broken, fix it until it is.


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

Click here to load this message in the networking platform