How to find unclosed datareader [Resolved]

Posted by Thiru under VB.NET on 8/28/2012 | Points: 10 | Views : 1975 | Status : [Member] | Replies : 5
Hi Friends,

How to find unclosed datareader ?

example:

Try
If SUM_COMMAMT < 1 Then
MessageBox.Show("No Records Found")
Return Nothing
Exit Function
End If
rdr = cmd.ExecuteReader
While rdr.Read
<do something....>
End While
Catch ex As Exception
MessageBox.Show(ex, "Support Panel", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If Con.State = ConnectionState.Open Then
Con.Close() : cmd.Dispose() : Con.Dispose()
rdr.Close()
End If

End Try



Note:
As per above code: If i get a message "No Records Found") (2nd line)
code will jump to Finally - right.
if so, i am getting an error in this line: rdr.Close()

so, i need to check the rdr before close the same.
How to do so ?




Responses

Posted by: Vuyiswamb on: 8/28/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 50

Up
0
Down

Resolved
I know you are using VB , so in VB you cant say != null or <> null , you can do something like this


IF(dr Isnot Nothing) then

//Do something
end if


Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 8/28/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
What is the Error ?

Thank you for posting at Dotnetfunda
[Administrator]

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 8/28/2012 [Member] Starter | Points: 25

Up
0
Down
The Error is:


Object reference not set to an instance of an object.


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Atal.Upadhyay on: 8/28/2012 [Member] Starter | Points: 25

Up
0
Down
i suppose you should use datareader.hasrow to check if datareader has any row present.

if (dr != null || dr.HasRows) {
// Do something
}


Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 8/28/2012 [Member] Starter | Points: 25

Up
0
Down
Thanks Vuyiswa :-)

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response