tags:

views:

110

answers:

2

Hi !

Here I am trying to write a function that returns me value based on the following condition...

IF

Variable is NULL return me DBNull.Value

ELSE

Convert Variable into DataType whose DataTypeCode has been provided to me as a parameter of function. eg.

Now while converting a variable into required DataType, an exception is thrown "FormatException was unhandled". Input string was not in a correct format.

How do I resolve this error please help.

Here is my code...

    public object ReturnDBNullOrRequiredDatatype(object IsThisObjectANull, TypeCode DataTypeCode)

    {
return IsThisObjectANull == null ? DBNull.Value : Convert.ChangeType(IsThisObjectANull, DataTypeCode);

    }

i am calling this function as ReturnDBNullOrRequiredDatatype(txtFirstName.text, TypeCode of an Integer variable) 2nd parameter is a data type into which first parameter is to be converted.

I am trying to return this value to

SqlParameter spAmount = new SqlParameter(...) spName.value=DbNull.value

or

SqlParameter spName = new SqlParameter(...) spAmount.value=Cint(txtName.text)

I am trying to do this with that function. I am making sure that the value being passed to that function will fulfill my conversion type but this error is still occuring.

+1  A: 

The FormatException is telling you that the object (in your case a string) cannot be converted to the requested type (in your case, integer). Probably the string being passed in is not a valid number, e.g. "abc" rather than "123"; or, for your DateTime case, it's not a valid date-time string for the current culture, e.g. "12/31/2009 12:34:56" when you're in a dd/MM/yyyy locale. Try passing an IFormatProvider, such as CultureInfo.InvariantCulture, that maps to the date-time format you are expecting your users to type in.

itowlson
...or it's a type that's not supported like a byte[] or a reference type.
Rick Strahl
Good point, though he says he's passing in txtFirstName.Text as the first arg, so I'm guessing it's a string.
itowlson
dont go over the name that it depicts. the data type that i m passing into which first value is to be converted will surely match the data type into which it has to be converted.
Shantanu Gupta
txtObject.text will always have value according to the 2nd parameter. If 2nd parameter is a string 1st parameter will always be a string, if it is 'int' then 1st parameter will have int as "123".
Shantanu Gupta
+1  A: 

The documentation for Convert.IsDBNull says the following:

Returns an indication whether the specified object is of type DBNull.

The documentation also specifically states that DBNull.Value is not equivalent to a null reference or string.Empty.

So the method should return false if you pass null or an empty string to it. I believe that what you really want to do is this:

if (IsThisObjectANull == null)
    return DBNull.Value;
else
    return Convert.ChangeType(IsThisObjectANull, DataTypeCode);

...or the shorter version:

return IsThisObjectANull == null ? DBNull.Value : Convert.ChangeType(IsThisObjectANull, DataTypeCode);
Fredrik Mörk
@Fredrik Mörk: yes i want this to be done as u wrote. But i m not clear with your first two lines.
Shantanu Gupta
@Shanantu: I am just trying to point out that there is a difference between `null` and `DBNull.Value`. `DBNull.Value` is not a null reference (it is a `DBNull` instance), and `Convert.IsDBNull` checks whether the passed object is a `DBNull` object. Obviously, a null reference is not a `DBNull` object, so the method should return false. Since you in your question stated that you wanted to check if the input was a null reference, `Convert.IsDBNull` cannot do the job for you.
Fredrik Mörk
@Fredrik Mörk: Do u mean to say, I cant return DBNull.Value which i think it should as it should also be of object type. Doesn't it comes under this category. I mean which is the parent class of DBNull.Value, is it an object or not. I think u seems to be right. Suggest me some other alternative to do the same in case it does'nt work.
Shantanu Gupta
@Shantanu: Object is parent class of all other classes, including DBNull.Value.
Roman Boiko
@Shanatu: your method could (and probably should) return `DBNull.Value`. All I am saying is that you cannot use `Convert.IsDBNull` to test for `null`. That is not what that method does. It tests for `DBNull` objects. This means that if you pass `null` to your original code, it will try to convert that null to the given type, which will fail for int, DateTime and all other value types. This is a bug in your code. However, I don't think this is the problem that you are facing right now (guessing from comments), since this would give you an `InvalidCastException`, not a `FormatException`.
Fredrik Mörk
So shouldn't it return DBNull.Value. As I am not sure now after reading Fredrik Mörk's Comments
Shantanu Gupta
@Fredrik Mörk: I am xtremly sry for misunderstooding u. Actually i was doing a blunder. Thx for correcting me. This error was troubling me for 3 days. Trillion ton thanks to all supportors. @Fredrik Mörk @Roman Boiko @itowlsonThx for ur patience.
Shantanu Gupta
Shantanu: You're welcome. Did it help? Is your problem solved?
Roman Boiko
Just give me few more moments. But u get my 1 bug
Shantanu Gupta
if (Convert.IsDBNull(IsThisObjectANull) == true) return DBNull.Value; else return Convert.ChangeType(IsThisObjectANull, DataTypeCode);
Shantanu Gupta
@Roman Boiko: Thx for the help my problem got resolved. There was a bug only. I was entering wrong input at the time of checking that;s why it was giving me format exception
Shantanu Gupta