Results 1 to 5 of 5

Thread: Error! Error! Error!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    Lesotho
    Posts
    22

    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.

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    What is the select statement for the bind?
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Try setting the default value in the dataset, something like.....

    Code:
    DataSet11.Table3.T1Column.DefaultValue = 0

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    Lesotho
    Posts
    22
    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)

  5. #5
    Lively Member Kenbuddy's Avatar
    Join Date
    Jul 2002
    Location
    Cypress, TX
    Posts
    96
    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width