logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Suggestion: Object cannot be cast from DBNull to other types: msg#00103

Subject: Suggestion: Object cannot be cast from DBNull to other types
First off, thank you to all that have created and helped improve the
FireBird .net driver, it's really made FireBird accessible and I really
appreciate the work that has been done.  I have a suggestion for future
versions of the FireBird.net driver.

I'm currently using 1.6.3.0 and I've checked the bug reports and change logs
to see if this has been addressed, but could not find it.

When a named parameter in commandtext is  is not added to the parameter
collection of the command object and then executed, FireBird doesn't report
(or apparently even check the validity of) the missing parameter name in a
sql exception, instead it throws a InvalidCastException.

It took me quite a while to figure out what was going on until I switched to
MS Sql server backend, (my app uses a data access layer so I can switch back
and forth) at which point I clearly could see I had screwed up my SQL UPDATE
query by forgetting to add a parameter.

As an example, I created a parameter in my update query called
"@MissingParameter" and deliberately did not add it to the command object to
generate the errors below when calling executenonquery:

I've pasted both exceptions below to show how the Firebird driver reports
this contrasted with how the Sql server driver reports this:

Error and stack trace from FireBird with missing parameter:
----------------------------------
Exception: Object cannot be cast from DBNull to other types.
System.InvalidCastException
Stack Trace:
   at System.DBNull.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ToInt32(Object value, IFormatProvider provider)
   at FirebirdSql.Data.Common.DbValue.GetInt32()
   at FirebirdSql.Data.Gds.XdrStream.Write(DbField param)
   at FirebirdSql.Data.Gds.XdrStream.Write(RowDescriptor descriptor)
   at FirebirdSql.Data.Gds.GdsStatement.Execute()
   at FirebirdSql.Data.Firebird.FbCommand.InternalExecute()
   at FirebirdSql.Data.Firebird.FbCommand.executeCommand(CommandBehavior
behavior, Boolean split)
   at FirebirdSql.Data.Firebird.FbCommand.ExecuteNonQuery()
-----------------------------------

By contrast, the same error as reported by MS SqlClient:
------------------------------------
Exception: Must declare the variable '@MissingParameter'.
System.Data.SqlClient.SqlException
Stack Trace:
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
------------------------------------

It would be handy and save some hair pulling if the FbCommand object did a
sanity check on the named parameters and reported the name back through the
exception.

- John   



-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click


<Prev in Thread] Current Thread [Next in Thread>