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
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
29 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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