Assume we have a datarow field, name, which evaluates to null. What is the result of the following two cases?
CASE A
string name = Convert.ToString(dr["name"]);CASE B
string name = dr["name"].ToString();
In the first case, Convert.ToString() will evaluate to null. In the second case, however, .ToString() will throw an exception. And it makes sense, doesn’t it? After all, “nothing” doesn’t have a ToString() function to execute!
So, do yourself a favor and use object.ToString() sparingly. Use is only when using non-null values. In cases where null is a possibility, use Convert.ToString().
As a developer I always use http://www.caseconvert.com/
Not high tech, but it’s nice to use
John says:
April 8, 2008 at 11:15 am
As a developer I always use http://www.caseconvert.com/
Not high tech, but it’s nice to use
hi,
the above comment by John is a SPAM post remove it please.
Case C:
string name = (string)dr["name"];
The Case A is needed with non string types, like int, float.
Where this case would give you an Exception, but it’s an performance friendly alternative.
Case D:
string intValueString = string.Format(“{0}”, dr2["IntValue"]);
If the field value is an int and can be null, this is an alternative to the Case A.
Sorry about the bad english
Greetings from California! I’m bored at work so I decided to check out your website on my iphone during lunch break. I enjoy the knowledge you present here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my mobile .. I’m not even using WIFI, just 3G .. Anyways, very good site!
I am really enjoying the theme/design of your web site. Do you ever run into any internet browser compatibility issues? A number of my blog readers have complained about my website not working correctly in Explorer but looks great in Chrome. Do you have any suggestions to help fix this problem?
My Firefox stopped working saying the server wasn’t found but Internet Explorers still works. I used to use firefox all the time but only recently it stopped working. I already checked the firewall and it allows firefox so i don’t know what’s wrong..
If you could mail me with a few ideas on how you made your website look this great, I would be grateful.