473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Data reader error

Expand|Select|Wrap|Line Numbers
  1. SqlConnection con = new SqlConnection(cn.getcon());
  2.             if (con.State == ConnectionState.Open)
  3.                 con.Close();
  4.             con.Open();
  5.             SqlCommand cmd;
  6.             SqlCommand cmd1;
  7.             SqlDataAdapter dr1;
  8.             SqlDataAdapter dr;  
  9.             cmd = new SqlCommand("select * from sabha00a where s00a0004<='" + A00D0001.Value.ToString("yyyy-MM-dd") + "'",con);
  10.             dr=cmd.ExecuteReader();
  11.             while (dr.Read())
  12.             {
  13.                 cmd1 = new SqlCommand("select * from milka00b where a00b0002=" + dr[0].ToString() + "",con);
  14.                 dr1 = cmd1.ExecuteReader(); 
  15.             }
  16.             con.Close();
//--------------------------------------------------------------------


Exception := Datareader already opened

I found this error in while loop. Please give me the solution for this error...
Apr 22 '10 #1
3 1399
tlhintoq
3,525 Expert 2GB
TIP: When you first created your question you were asked to wrap your code with [code] tags.

It really does help a bunch. Look how much easier it is to read now that someone has done it for you. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Apr 22 '10 #2
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. if (con.State == ConnectionState.Open) con.Close();
  2.             con.Open();
I'm out of my element here but if I had to guess it is because you are making an assumption. Just because you order a con.Close() you assume it actually happens. What if it doesn't for some reason? You are not checking for the possibility of a less than perfect world.

Expand|Select|Wrap|Line Numbers
  1. if (con.State == ConnectionState.Open)   con.Close();
  2. if (con.State == ConnectionState.Open)
  3. {
  4.    MessageBox.Show("Unable to close connection", "Error", MessageBoxButtons.Ok, MessageBox.Icon.Error);
  5. }
  6.             con.Open();
Apr 22 '10 #3
jkmyoung
2,057 Expert 2GB
you probably want:
if (con.State != ConnectionState.Close) con.Close();

Don't forget there are more states than just open and close.

See: http://msdn.microsoft.com/en-us/libr...v=VS.100).aspx
You were probably in fetching mode, or another state.
Apr 23 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
1
by: Stephen | last post by:
I'm having terrible trouble working out where I have gone wrong in my code below. In the data layer I have two methods and i'm basically returning a command and using it and then returning a...
9
by: Mike L | last post by:
I tried a sample of code in MSDN magazine, but now I'm stuck. What code has the best performance to populate a Data Grid with a SP? Below is the code I have, which might be completing the wrong...
5
by: Tammy | last post by:
Hi, I have an aspx app which needs to post data to a form and read the response. I am confused on whether I should be using the get_url using "POST" method or the post_url using "GET" method. ...
2
by: Keith Avery | last post by:
We have an ASP.NET application. This is loading data using XML from another ASP web site. It all works fine for small XML data files. For larger XML data file being streamed from the ASP web...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
15
by: mosipenko | last post by:
I used to have a problem with DBNull, but now my issue is: System.InvalidOperationException: Invalid attempt to read when no data is present Here's my code behind: Private Sub...
5
by: Kenny D | last post by:
I am having trouble sending streams of data from one socket to another using the following code. When the transmitter is running on the same machine as the receiver, the data transmits...
0
by: nidhisahu | last post by:
Hello All, i am open this URL( http://id.skootit.com/WorkspaceService/ListInvitations?actor=http://goku.id.skootit.com/) directly in browser with enter credential then its open and show...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.