Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server VB.NET
Dim fcs As String
        fcs = "select fname,dept from nstudent where stid = '" & txt_sid.Text & "'"
        scmd1 = New SqlCommand(fcs, con)
        dr = scmd1.ExecuteReader
        Dim n As Byte
        n = dr.Read
        If (n > 0) Then
            txt_sname.Text = dr.Item(0)
            cmb_dept.Text = dr.Item(1)
        Else
            MsgBox("Not Found")
        End If
        If Not dr.IsClosed Then dr.Close()
The above code i have used. if the given input is present it returning otherwise the program exit and displaying the following error.
 
There is already an open DataReader associated with this Command which must be closed first
 
pls anyone give the solution..
Posted 14-Mar-13 10:38am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

From what you have posted, it looks like you are not disposing of scmd1,
 
If you dispose of this after you close the datareader it might solve your problem.
  Permalink  
Comments
Marco Bertschi at 14-Mar-13 16:18pm
   
It could also be that he does not close the datareader dr and then the datareader stays open and can't be used to execute another command.
 
cheers,
Marco Bertschi
Pheonyx at 14-Mar-13 16:24pm
   
From his code example he is closing it:
 
If Not dr.IsClosed Then dr.Close()
Marco Bertschi at 14-Mar-13 16:27pm
   
Whoops, I overviewed this one - Nevermind.
 
cheers,
Marco Bertschi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 Kornfeld Eliyahu Peter 407
2 Maciej Los 369
3 DamithSL 221
4 OriginalGriff 218
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100