Solved

Invalid Cast from System.DBNull

Posted on 2003-11-15
3
1,019 Views
Last Modified: 2008-01-09
Hi Thanks for clicking,

Get this error "Invalid cast from System.DBNull to System.Windows.Form.CheckState"

This happens in the VB6_AddADODataBinding() routine

at the function ADOBind_Adodc14.Add(ClientChange, "CheckState", "ClientChange", StdCheckBoxFmt, "ClientChange")

where ClientChange is a simple checkbox

Using ADO (and not ADO.net) because converted over from Access 2000 to VB6 and then to vb.net.

Problem seems to be that the Table that ADO is binding to is deleted (hence DBNull), and is only filled by running a Query later in the code.

Anybody any ideas how to get round this?? Your thoughts would be very very welcome because I've been struggling with this for some time.
0
Comment
Question by:KBanner
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
krznpsk earned 125 total points
ID: 9755294
What's more likely is that ClientChange's value is NULLABLE in the database, and you are coming up with NULL (neither true nor false) values for the checkbox.

DBNull is a value indicating that a field has no value, so if it's a NULLABLE bit or boolean field, DBNull would represent an "unknown" value.

DBNull cannot be cast to most datatypes.

Try adding this conditional logic:

If Not ClientChange Is System.DBNull.Value Then
       ADOBind_Adodc14.Add(ClientChange, "CheckState", "ClientChange", StdCheckBoxFmt, "ClientChange")
End If

A better solution would be to modify the column in your database NOT to accept NULLs, instead specifying a DEFAULT value (in this case True or False).
0
 

Author Comment

by:KBanner
ID: 9876582
Hi krznpsk,

Sorry for late reply, went trying things out and forgot to return. Only saw it when I came in.

Problems with changing the database is want to keep it in same format as the original application before conversion to vb.net

As a courtesy, and for shared learning, this is how I got around it. I set up a dummy record within the table, set ADO Databindings for all checkboxes, and then delete records.

Have awarded points, since you took the time to help, and even pointed me in the right direction. Thank you!!
0
 
LVL 4

Expert Comment

by:krznpsk
ID: 9876937
Glad you got it working.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now