Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
62 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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