Solved

Invalid Cast from System.DBNull

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

778 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