I'm coding button which name 'Load'. I coded it like this,

Try
            Using cmd = New MySqlCommand(Query, con)
                Query = "SELECT first_name FROM custormer WHERE custormer_id='" & txtcustormeridforloanform.Text & "'"
                dr = cmd.ExecuteReader()
                While dr.Read()
                    Me.txtcustormername.Text = Convert.ToString(dr("first_name"))
                    cmd.ExecuteNonQuery()
                End While
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

I want to do this, there is text box name txtcustormer_id. If I type custormer id and click load button, I want to show custormer name to txtcustormer_name text box. but unfortunately firstly it give
The commandText property has not been properly initialized.
and after i click ok button on error message and again click load button it show correct custormer name. but second time I enter another custormer id and click load button. it show this error mesasge,
There is already an open datareader associated with this connection which must be closed first.
Can anyone help me ?

At the time you do

Using cmd = New MySqlCommand(Query, con)

Query does not have a value. Assign the query string to Query prior to that statement.

It doesn't matter whether it is public or private as long as it is in scope when you use it. Have a look at this code snippet. Note that at the end of the loop I do

rdr.Close()

once I am done reading the data. It's a good idea to keep things open for as short a time as possible. Similarly, make the scope of variables as restrictive as possible. The examples in the code snippet use MSSQL but the sequence of operations will be the same for MySQL.

Edited 10 Months Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.