there is already an open datareader associated with this command which must be closed

Posted on 2009-05-16
Last Modified: 2012-05-07
there is already an open datareader associated with this command which must be closed
I am closing reader.Close() for each datareaer.
code is working in a single mechine properly.
 more request comes from two or more mechine   then i get an Error
Error-->:there is already an open datareader associated with this command which must be closed first.
Protected Sub BtnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
            Dim sss As String = cn.State.ToString()
            Session("UId") = TBUserName.Text
            Dim ID As String = TBUserName.Text
            cmd = New SqlCommand
            cmd.Connection = cn
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "sp_administration"
            cmd.Parameters.Add("@transid", SqlDbType.Int).Value = 1
            cmd.Parameters.Add("@userid", SqlDbType.VarChar, 50).Value = TBUserName.Text
            cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = TBPassword.Text
            rd = cmd.ExecuteReader()  ///Error
            If rd.Read() Then
                Session("AId") = rd.GetValue(0).ToString()
                Session("CenterId") = rd.GetValue(3)
                Session("CID") = rd.GetValue(3)
                Dim userType As String = rd.GetValue(2).ToString()
                Session("UId") = TBUserName.Text
                Session("Utype") = userType.ToString()
                If userType = "0" Then
                End If
                If userType = "1" Then
                End If
                If userType = "2" Then
                End If
                lblmsg.Text = "Invalid Userid/password"
            End If
        End Sub
Connection is openened in a session start event

Open in new window

Question by:logicslab
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24402439
Can you post your declaration for the variable "cmd"?

Accepted Solution

ajolly earned 250 total points
ID: 24402472
Use closed behavior.

rd = cmd.ExecuteReader(CommandBehavior.CloseConnection)
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 250 total points
ID: 24403313

Expert Comment

ID: 24409118

You need to close the connection (cn) aswell.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity Run 3 sliders vertically as opposed to horizontally 9 21
Entity Framework 7 41
Message not shown 5 49
How to make a GridView cell hyperlinked using C# ? 3 33
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now