-
Error! Error! Error!
I'm getting this error message:
An unhandled exception of type 'System.InvalidCastException' occurred in system.windows.forms.dll
Additional information: Object cannot be cast from DBNull to other types.
This occurs when I click on the add new record button on the form on which I have check box controls bound to the dataset. I've realised that this maily happens because the fields in the database are null. In the database, the field type is 'bit'. I'm using SQL Server 2000. I tried setting the default values to zero in the database but still problem is not resolved. Somebody pls help. I'm stuck.
-
What is the select statement for the bind?
-
Try setting the default value in the dataset, something like.....
Code:
DataSet11.Table3.T1Column.DefaultValue = 0
-
Here is my binding code
Dim da as New SqlDataAdapter("SELECT * FROM Customers", cnConnection)
Dim CMB as New SqlCommandBuilder(da)
Dim ds as New DataSet
da.Fill(ds)
chkApproved.Databindings.Add("Checked", ds.Tables( 0), "Approved")
........
On btnAddNewClick
me.bindingcontext(ds.tables(0)).Addnew
'Immediately, I get an error
I've just tried setting a default value but i still get the same error message, unless I've put the statement at the wrong place (just before the bind statement)
-
The problem is caused from binding certain types of controls, which can't display DBNull values properly, to the dataset. Two examples are the DateTimePicker, and the Checkbox. Here is an MSDN article that describes the problem:
http://support.microsoft.com/default.aspx?scid=kb;en-us;313513
I was having the same error and found that in my case, it was a Checkbox. I didn't use the solution provided in the MSDN article. I just made sure that the field in the dataset that the checkbox was bound to was not null before doing the "resumebinding" command on the binding context object (or in your case, AddNew).