Problems with DataReader!!

Hi, can someone please help my, I have experienced some realy weird stuff. This datareader hasn't been closed, but when I try to run the program, I get an error message, stating.

"INVALID ATTEMPT TO READ WHILE READER IS CLOSED"

Is their anyone that can see something in my code that I'm missing.

Thank you.

Or any suggestions

Try

Dim myreader As OleDb.OleDbDataReader

Dim mycomm As New OleDb.OleDbCommand("SELECT * from job where project = '" & str2 & "'", conn)

If conn.State = ConnectionState.Open Then

conn.Close()

End If

conn.Open()

myreader = mycomm.ExecuteReader(CommandBehavior.CloseConnection)

While myreader.Read()

str = myreader("jobid")

mdlinstallair.jobval() = str

ListBox1.Visible = False

ListBox2.Visible = True

End While

myreader.Close()

Catch ex As Exception

MessageBox.Show("THIS ONE ASWELL" & ex.Message)

End Try

conn2.Close()

conn.Close()



Answer this question

Problems with DataReader!!

  • Eadd

    Try setting the commandtype to CommandType.Text.

  • coloradovista

    If it has no rows, it wont Read()

  • Mynor Ivan Muralles MSFT

    I am having exactly the same problem, getting the same error message, and it is happening when I run my application as well as when I attempt to view the contents of the view I am attempting to access in the code, in SQL Management Studio. Funny thing is, it works just fine when I am in my office, but fails when I am at home on one of my computers but not the other. The one it works on has SQL 2000 on it and the one that fails has 2k5 on it.
  • Isaac Strack

    First, I don't understand why you're closing the connection and reopening it... are you trying to purge a previous DataReader

    Next, before you try to reference a DataReader, you need to determine if there are rows being returned.

    If Dr.HasRows Then...

    hth



  • fap99

    Can you post the call stack and the detailed message associated with the exception

  • AlinaMaria

    nope, no luck sorry.

    I saw someone with the same prolems on some of the other forums, but now one was able to help him.

    That's why I pop in here.

    To see if some of there's any advanced hardcore programmer some where that might be able to help me.


  • VistaGeek

    The way I program database operations like this is:

    SqlConnection conn = new SqlConnection("Insert Connectionstring Here");

    SqlCommand cmd = new SqlCommand("Insert command text here", conn);

    cmd.CommandType = CommandType.Text;

    SqlDataReader reader = null;

    try

    {

    conn.Open();

    reader = cmd.ExecuteReader();

    while(reader.Read())

    {

    }

    reader.Close();

    conn.Close();

    }

    catch(SqlException ex)

    {

    // Handle the Exception

    }

    finally

    {

    // Detect open connections and readers and close them

    }

    I usually don't do any UserInterface actions in the database operations.



  • Problems with DataReader!!