Member Avatar for සශික

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.

Member Avatar for සශික

bro,
I assign query as public variable. is that you mean ?

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.

Member Avatar for සශික

hey where can I contact you ?

Member Avatar for සශික

can I message you via daniweb ? where ?
I'm looking for conatct you with quick :)

You can PM me at Daniweb. What is the problem with talking in this thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.