Solved

Invalid Cast from System.DBNull

Posted on 2003-11-15
3
1,022 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

19 Experts available now in Live!

Get 1:1 Help Now