Data reader error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • magic987654
    New Member
    • Apr 2010
    • 2

    Data reader error

    Code:
    SqlConnection con = new SqlConnection(cn.getcon());
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                SqlCommand cmd;
                SqlCommand cmd1;
                SqlDataAdapter dr1;
                SqlDataAdapter dr;  
                cmd = new SqlCommand("select * from sabha00a where s00a0004<='" + A00D0001.Value.ToString("yyyy-MM-dd") + "'",con);
                dr=cmd.ExecuteReader();
                while (dr.Read())
                {
                    cmd1 = new SqlCommand("select * from milka00b where a00b0002=" + dr[0].ToString() + "",con);
                    dr1 = cmd1.ExecuteReader(); 
                }
                con.Close();
    //--------------------------------------------------------------------


    Exception := Datareader already opened

    I found this error in while loop. Please give me the solution for this error...
    Last edited by tlhintoq; Apr 22 '10, 03:03 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you first created your question you were asked to wrap your code with [code] tags.
    [imgnothumb]http://files.me.com/tlhintoq/10jihf[/imgnothumb]
    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.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Code:
      if (con.State == ConnectionState.Open) con.Close();
                  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.

      Code:
      if (con.State == ConnectionState.Open)   con.Close();
      if (con.State == ConnectionState.Open)
      {
         MessageBox.Show("Unable to close connection", "Error", MessageBoxButtons.Ok, MessageBox.Icon.Error);
      }
                  con.Open();

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        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.

        Comment

        Working...