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
12 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
Comment Utility
Have you closed the other data reader before running this code?
0
 

Author Comment

by:karthik80c
Comment Utility
Other Data reader means
0
 
LVL 44

Assisted Solution

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

Accepted Solution

by:
karthik80c earned 0 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

14 Experts available now in Live!

Get 1:1 Help Now