?
Solved

Invalid Cast from System.DBNull

Posted on 2003-11-15
3
Medium Priority
?
1,039 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
krznpsk earned 375 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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