problems with parsing & format in ConvertEventArgs when field value is DBNULL

N

Neo

I have a table Contacts with a field "Male" which is of type Bit (SQL Server).

I've got two radio buttons in a panel, & one of them is bound to the above field, (so that one can see if the contact is a Male of FeMale - determine or change the gender).
Below is the coding.

BUT, if i have a record where the Male field is "NULL", then all hell breaks lose, & i get the below error

An unhandled exception of type 'System.InvalidCastException' occurred in system.windows.forms.dll
Additional information: Object cannot be cast from DBNull to other types.

Any solutions??? (i'm totally lost)

Code ->

Dim dbnGender As New Binding("Checked", poDataTable, "Male")
AddHandler dbnGender.Format, AddressOf MaleToBollean
AddHandler dbnGender.Parse, AddressOf BooleanToMale
rdoMale.DataBindings.Add(dbnGender)

Protected Sub MaleToBollean(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
e.Value = e.Value
Catch exp As Exception
MessageBox.Show("Data entry error: " & exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Protected Sub BooleanToMale(ByVal sender As Object, ByVal e As ConvertEventArgs)
Try
e.Value = IIf(e.Value, 1, 0)
Catch exp As Exception
MessageBox.Show("Data entry error: " & exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top