Solved

Invalid Cast from System.DBNull

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

718 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