0
votes

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.

2
Here I am trying to create a function that can convert any datatype into another datatype as desired by a programmer. Parameter can be string or int or datetime. - Shantanu Gupta
Please provide input parameters for your method with which it fails - Roman Boiko
ReturnDBNullOrRequiredDatatype(txtAmount.text,TypeCode.Int); - Shantanu Gupta
`static void Main(String[] args){ Console.Write(Convert.ChangeType("123", TypeCode.Int32)); }' works without exceptions. Another example you provided also works fine. - Roman Boiko
Please provide code example that fails, or input parameter values for which you get an exception. - Roman Boiko

2 Answers

1
votes

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);
1
votes

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.