473,467 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Data reader error

2 New Member
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 1400
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.