Solved

MySQL database throwing error as  There is already an open DataReader associated with this Connection which must be closed first.

Posted on 2016-10-04
5
48 Views
Last Modified: 2016-10-08
Hi Experts,

Am using the following code to check user login below is my code


 //suppose col0 and col1 are defined as VARCHAR in the DB
                string query = "SELECT * FROM user WHERE username = '" + TextBox1.Text + "' AND password = '" + TextBox2.Text + "'";
                var cmd = new MySqlCommand(query, dbCon.Connection);
                // cmd.ExecuteReader();
                MySqlDataReader rdr = cmd.ExecuteReader(); ;
                 var da = new MySqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Session["ValidateLogOn"] = true;
                    Session["UserName"] = Convert.ToString(dt.Rows[0]["username"]);
                    Session["UserId"] = Convert.ToString(dt.Rows[0]["ID"]);
                    Session["Email"] = Convert.ToString(dt.Rows[0]["Email"]);
                    Response.Redirect("Dashboard.aspx");
                }

Open in new window



But throwing error as
There is already an open DataReader associated with this Connection which must be closed first.
0
Comment
Question by:karthik80c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41827573
Have you closed the other data reader before running this code?
0
 

Author Comment

by:karthik80c
ID: 41827575
Other Data reader means
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 41827584
another DataReader, not the one you have in your code snippet
1
 

Accepted Solution

by:
karthik80c earned 0 total points
ID: 41827610
oh ok ANdy thanks for your Help . I think i didn't closed my Reader that's the problem . Got it Thanks Buddy. Please use Using statement to avoid this kind of problem  

using (MySqlDataReader rdr = cmd.ExecuteReader())
                {



}

Open in new window

0
 

Author Closing Comment

by:karthik80c
ID: 41834917
Please use the using statement  or close the reader . Its best to use using statement when error occurs it automatically close the Reader

using (MySqlDataReader rdr = cmd.ExecuteReader())
                {

}
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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