Im currently doing a entrance exam system. And I have a problem in adding abstract question

When I save data I get some error. But when I save the data with image in the picturebox it saves sucessfully. Can you help me in saving the data even the picturebox is empty?

Please post your code on what have you tried and where you are having a problem

When a picture box is empty, the Image property is null. I'd wager that however you're saving the data doesn't take into account that the image might be a null object.

You can either change how you save the data to account for nulls, or ensure that the picture box always has at least a default image.

Edited 1 Year Ago by deceptikon

Private Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
        Dim WA As Short

        con.Open()
        Using cmd As New SqlClient.SqlCommand("insert into quesTable(subject,question,A,B,C,D,answer,imagequestion) values('" & cbsubject.Text & "','" & rtboxquest.Text & "','" & achoicetb.Text & "','" & bchoicetb.Text & "','" & cchoicetb.Text & "','" & dchoicetb.Text & "','" & anstb.Text & "',@pboximage)", con)
            cmd.Parameters.Add(New SqlClient.SqlParameter("@pboximage", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
            WA = cmd.ExecuteNonQuery
        End Using
        con.Close()

        If (WA > 0) Then
            MsgBox("The question has been saved!")
            showdata()
        End If

    End Sub

Here is my code and Im always getting error in cmd.Parameters.Add(New SqlClient.SqlParameter("@pboximage", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName) saying File not found exception was unhandled.

That says you need to use the

Try
' Your code here
Catch ex As Exception
' Handle the error here
End Try

or perhaps you can use the If statement and also check all the possibility errors that may occur

saying File not found exception was unhandled

Okay, what's a, and how have you populated/verified the FileName property? Clearly it doesn't refer to a valid file, so you need to figure out why. I wouldn't recommend simply wrapping the code in a try..catch block, because the exception suggests an underlying logic error.

This article has been dead for over six months. Start a new discussion instead.