RivKin
asked on
Object reference not set to an instance of an object ERROR, PLEASE HELP!!!!
Hi Everybody, I’m a new user with VB.net
I have created a procedure so that each time the user leave the comboBox Control this code happens:
Private Sub MyComboBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTapeId.Leave
Dim sqlStr As String = "SELECT * FROM tapes where tape_id = " & cmbTapeId.Text
' Create data adapter object
Dim dr As SqlDataReader
dc.Connection = SqlConnection1 'SqlConnection1 was created in the VS desigh ' THIS IS WHERE THE ERROR COME UP
dc.CommandText = sqlStr
dc.CommandType = CommandType.Text
dr = dc.ExecuteReader()
While dr.Read()
'reading from the datareader
txtTapeDecs.Text = dr(dr.GetOrdinal("tape_des cription") ).ToString ()
End While
dc = Nothing
End Sub
This Error Appears only on the second time it enters the Sub.
What to do?
I have created a procedure so that each time the user leave the comboBox Control this code happens:
Private Sub MyComboBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbTapeId.Leave
Dim sqlStr As String = "SELECT * FROM tapes where tape_id = " & cmbTapeId.Text
' Create data adapter object
Dim dr As SqlDataReader
dc.Connection = SqlConnection1 'SqlConnection1 was created in the VS desigh ' THIS IS WHERE THE ERROR COME UP
dc.CommandText = sqlStr
dc.CommandType = CommandType.Text
dr = dc.ExecuteReader()
While dr.Read()
'reading from the datareader
txtTapeDecs.Text = dr(dr.GetOrdinal("tape_des
End While
dc = Nothing
End Sub
This Error Appears only on the second time it enters the Sub.
What to do?
Since you disposed of it, you cannot use it again
The dc was created outside this sub. The first time you use it, it is OK. But on the last line of the sub, you set it to nothing. So next time you try to use it you get the error.
Sorry, I was typing while Ronald was posting.
it would also work if you declared dc inside your sub, then you can keep the dc = nothing
ASKER
o.k.
I added this line:
Dim dc As SqlClient.SqlCommand = New SqlClient.SqlCommand
and the problem fixed.
but now the same error happens for this line: (also on the second run)
dr = dc.ExecuteReader()
I added this line:
Dim dc As SqlClient.SqlCommand = New SqlClient.SqlCommand
and the problem fixed.
but now the same error happens for this line: (also on the second run)
dr = dc.ExecuteReader()
I cannot see anything obviously wrong within this code, and it ought now to be self-contained. Strictly, having declared dr within the sub, you ought to set it to Nothing at the end, but I cannot see how not doing so should cause the error you now describe on the second pass. That is one thing you might like to try, though.
The other thing to try is to see what happens if you follow the alternative route originally suggested. That is, rather than declaring dc afresh at the start of every call of the sub and setting it to Nothing at the end, you remove (or comment out) both
your newly added line
Dim dc As SqlClient.SqlCommand = New SqlClient.SqlCommand.
and
dc = Nothing
The other thing to try is to see what happens if you follow the alternative route originally suggested. That is, rather than declaring dc afresh at the start of every call of the sub and setting it to Nothing at the end, you remove (or comment out) both
your newly added line
Dim dc As SqlClient.SqlCommand = New SqlClient.SqlCommand.
and
dc = Nothing
can you repost your entire code again with the changes you have made as mentioned above
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ronald
Just for interest, could you please explain what was happening here?
Although the dr was neither closed nor explicitly set to Nothing, it was declared within the sub and so, I thought, should have automatically been disposed of when the sub was exited. But if not closing it caused the problem, and closing it solved the problem, it seems as if it must have persisted between calls to the sub. Which makes me think my understanding of scope must be wrong , or incomplete. And how does it all square with dim-ming dr again next time the sub is called?
Roger
Just for interest, could you please explain what was happening here?
Although the dr was neither closed nor explicitly set to Nothing, it was declared within the sub and so, I thought, should have automatically been disposed of when the sub was exited. But if not closing it caused the problem, and closing it solved the problem, it seems as if it must have persisted between calls to the sub. Which makes me think my understanding of scope must be wrong , or incomplete. And how does it all square with dim-ming dr again next time the sub is called?
Roger
With most objects that is the case but the datareader is different because it is connected (the only one in ado.net) and it cannot be disposed off because the connection is still open.
also note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You will not be able to execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.
also note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You will not be able to execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.
Ronald
Thanks very much
Roger
Thanks very much
Roger
dc = Nothing