Dear All,
I have the following code for List box selected index changed. It works When I click once on the list box. But when I repeat it following error appears.
" Object reference not set to an instance of an object"
An unhandled exception of type 'System.NullReferenceException' occurred in ImagesDB.exe
Additional information: Object reference not set to an instance of an object.
the original code is as follows:-
Private Sub lstPartNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPartNo.SelectedIndexChanged
'define the query
Dim sql As String = "SELECT ProductID,ProductName,ProductImage FROM Products WHERE ProductID = @ProductID"
'create a connection to the database
'Dim con As New OleDbConnection(conString)
'create the command
Dim cmd As New OleDbCommand(sql, con)
cmd.CommandType = CommandType.Text
'create the parameter
Dim prmID As New OleDbParameter
With prmID
.ParameterName = "@ProductID"
.Value = lstPartNo.SelectedItem.ToString
End With
cmd.Parameters.Add(prmID) 'add this to the command
'we need a dataadapter and a dataset to grab the image
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet("Picture")
Try
con.Open() 'open the connection
da.Fill(ds) 'fill the dataset
If ds.Tables(0).Rows.Count = 0 Then
MessageBox.Show("There is no image with that ID in the database")
Exit Sub
End If
'txtPartNo.Text = cmd.Parameters.Item("ProductID") 'bail out if we don't have a picture
'if we got here we are good to go. Create the buffer and fill it
Dim buffer() As Byte = CType(ds.Tables(0).Rows(0)("ProductImage"), Byte())
'create memory stream from this array of bytes
Dim str As New MemoryStream(buffer)
'set the image property of the picture box to the image in the stream
pic.Image = Image.FromStream(str)
Dim db_reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
If db_reader.HasRows Then
db_reader.Read()
txtPartNo.Text = db_reader.Item("ProductID").ToString
txtDescription.Text = db_reader.Item("ProductName").ToString
End If
Catch ex As Exception
MessageBox.Show("There was a problem reading the database." & ControlChars.CrLf & ex.Message)
pic.Image = Nothing
'Exit Try
Finally
If con.State = ConnectionState.Open Then con.Close()
con.Dispose() : con = Nothing
cmd.Dispose() : cmd = Nothing
da.Dispose() : da = Nothing
ds.Dispose() : da = Nothing
End Try
End Sub
End Class
Thanks in Advance
I have the following code for List box selected index changed. It works When I click once on the list box. But when I repeat it following error appears.
" Object reference not set to an instance of an object"
An unhandled exception of type 'System.NullReferenceException' occurred in ImagesDB.exe
Additional information: Object reference not set to an instance of an object.
the original code is as follows:-
Private Sub lstPartNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPartNo.SelectedIndexChanged
'define the query
Dim sql As String = "SELECT ProductID,ProductName,ProductImage FROM Products WHERE ProductID = @ProductID"
'create a connection to the database
'Dim con As New OleDbConnection(conString)
'create the command
Dim cmd As New OleDbCommand(sql, con)
cmd.CommandType = CommandType.Text
'create the parameter
Dim prmID As New OleDbParameter
With prmID
.ParameterName = "@ProductID"
.Value = lstPartNo.SelectedItem.ToString
End With
cmd.Parameters.Add(prmID) 'add this to the command
'we need a dataadapter and a dataset to grab the image
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet("Picture")
Try
con.Open() 'open the connection
da.Fill(ds) 'fill the dataset
If ds.Tables(0).Rows.Count = 0 Then
MessageBox.Show("There is no image with that ID in the database")
Exit Sub
End If
'txtPartNo.Text = cmd.Parameters.Item("ProductID") 'bail out if we don't have a picture
'if we got here we are good to go. Create the buffer and fill it
Dim buffer() As Byte = CType(ds.Tables(0).Rows(0)("ProductImage"), Byte())
'create memory stream from this array of bytes
Dim str As New MemoryStream(buffer)
'set the image property of the picture box to the image in the stream
pic.Image = Image.FromStream(str)
Dim db_reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
If db_reader.HasRows Then
db_reader.Read()
txtPartNo.Text = db_reader.Item("ProductID").ToString
txtDescription.Text = db_reader.Item("ProductName").ToString
End If
Catch ex As Exception
MessageBox.Show("There was a problem reading the database." & ControlChars.CrLf & ex.Message)
pic.Image = Nothing
'Exit Try
Finally
If con.State = ConnectionState.Open Then con.Close()
con.Dispose() : con = Nothing
cmd.Dispose() : cmd = Nothing
da.Dispose() : da = Nothing
ds.Dispose() : da = Nothing
End Try
End Sub
End Class
Thanks in Advance