I am developing a simple family card vb.net application with sqlexpress database. I am struck, please help:
When user save member, there are two Fields 1. Date of Birth, 2. Date of Baptism. He can skip both. So that I use the following statement:
If Not IsDate(DOBDatepik.Text) Then
sqlcmd.Parameters.Add("@DOB", SqlDbType.DateTime).Value = DBNull.Value
Else
sqlcmd.Parameters.Add("@DOB", SqlDbType.DateTime).Value = DOBDatepik.Value
End If
it works. Now I create new form with Datagrid to find families.. Here is one statement when user Double click on any cell, a new form will display the current record. For that I use following statement:
I have not copied the whole text. This only for clarification
Private Sub familyGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles familyGridView.CellDoubleClick
Dim int As Integer = Me.familyGridView.CurrentCell.RowIndex
FamilyDetails.DOBDatepik.Value = Me.familyGridView.Item(6, int).Value
FamilyDetails.baptismDatepik.Value = Me.familyGridView.Item(12, int).Value
FamilyDetails.Show()
End Sub
it works, but where there is null date, it give the following error
Conversion from type 'DBNull' to type 'Date' is not valid.
and one more thing.. there is one image field in my table. So I use following statement to convert images into binary data:
Private Function ConvertMyImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream()
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim mbytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(mbytes, 0, mstream.Length)
Return mbytes
End Function
This function convert image to binary and save into Database. DataGrid also displays the images, no issue. Issue is that when I Double click on any cell and data shows into new form the following statement not working:
FamilyDetails.MemberPhotobox.Image = Me.familyGridView.Item(13, int).Value
it gives me the error: Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'.
So please help me to resolve this. Null Date is not passing from DataGrid to new form, and Image which are as binary data in the table are also giving error while sedning to new form.