?
Solved

Object reference not set to an instance of an object ERROR, PLEASE HELP!!!!

Posted on 2005-05-11
12
Medium Priority
?
312 Views
Last Modified: 2010-04-23
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_description")).ToString()
        End While

        dc = Nothing

    End Sub


This Error Appears only on the second time it enters the Sub.

What to do?


0
Comment
Question by:RivKin
12 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13978387
loose this

 dc = Nothing

0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13978396
Since you disposed of it, you cannot use it again
0
 
LVL 34

Expert Comment

by:Sancler
ID: 13978410
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 34

Expert Comment

by:Sancler
ID: 13978426
Sorry,  I was typing while Ronald was posting.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13978487
it would also work if you declared dc inside your sub, then you can keep the dc = nothing
0
 

Author Comment

by:RivKin
ID: 13979573
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()


0
 
LVL 34

Expert Comment

by:Sancler
ID: 13982543
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

0
 
LVL 9

Expert Comment

by:checoo
ID: 13983617
can you repost your entire code again with the changes you have made as mentioned above
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 2000 total points
ID: 13984804
you haven't closed your dr

While dr.Read()
            'reading from the datareader
            txtTapeDecs.Text = dr(dr.GetOrdinal("tape_description")).ToString()
        End While
dr.close

        dc = Nothing
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14005936
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
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 14016574
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.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 14016660
Ronald

Thanks very much

Roger
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question