Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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

VB
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:

VB
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:

VB
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.
Posted

1 solution

The problem is that null in C# is a completely different object from NULL in database. Whenever the database return a null value it's represented by a DBNull.Value[^]. This value cannot be assigned to a nullable variable directly. Instead you need to investigate if the value from DB is DBNull and then make proper assignment.

The same kind of problem is with the image. Somewhere in the code you try to use the byte array without converting it to an Image object.

Example added
C#
DataTable dataTable = new DataTable();
dataTable.Columns.Add("IntegerColumn1", typeof(int));
System.Data.DataRow dataRow;
int? integerNumber;

dataRow = dataTable.NewRow();
if (dataRow["IntegerColumn1"] == DBNull.Value) {
   integerNumber = null;                             // null will be assigned
} else {
   integerNumber = (int)dataRow["IntegerColumn1"];
}

dataRow["IntegerColumn1"] = 5;
if (dataRow["IntegerColumn1"] == DBNull.Value) {
   integerNumber = null;
} else {
   integerNumber = (int)dataRow["IntegerColumn1"];   // 5 will be assigned
}
 
Share this answer
 
v3
Comments
Member 10279246 28-Jul-15 3:54am    
will you please provide any code.... and it is vb.net application
Wendelius 29-Jul-15 2:29am    
Have a look at the modified answer.
Member 10279246 29-Jul-15 3:31am    
THANKS DEAR.. IT WORKS
Wendelius 29-Jul-15 3:44am    
Glad it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900