Member Avatar for සශික

This is my search button code. but it give error.
I enter custormer id and click search button it give this error "The text command text has not been properly intialized"
and again I click on search button. that time it show correct data row.
but again I entered new custormer id and click search button. it show early result. again when I click on search button it show correct data entry. this is my code:

Try
            con.Open()
            Using cmd = New MySqlCommand(Query, con)
                Query = "select first_name,last_name,NIC_NO,c_address1,c_address2,c_address3,c_telephoneNO,membership_date,business_name,g_name,g_nicno,g_address1,g_address2,g_address3,g_telephoneNO from custormer where custormer_id='" & txtcustormerids.Text & "'"
                dr = cmd.ExecuteReader()
                While dr.Read()
                    Me.txtfirstnames.Text = Convert.ToString(dr("first_name"))
                    Me.txtlastnames.Text = Convert.ToString(dr("last_name"))
                    Me.txtnicnos.Text = Convert.ToString(dr("NIC_NO"))
                    Me.txtcaddress1s.Text = Convert.ToString(dr("c_address1"))
                    Me.txtcaddress2s.Text = Convert.ToString(dr("c_address2"))
                    Me.txtcaddress3s.Text = Convert.ToString(dr("c_address3"))
                    Me.txtctelephonenos.Text = Convert.ToString(dr("c_telephoneNO"))
                    Me.txtmembershipdates.Text = Convert.ToString(dr("membership_date"))
                    Me.txtbusinessnames.Text = Convert.ToString(dr("business_name"))
                    Me.txtgnames.Text = Convert.ToString(dr("g_name"))
                    Me.txtgnicnos.Text = Convert.ToString(dr("g_nicno"))
                    Me.txtgaddress1s.Text = Convert.ToString(dr("g_address1"))
                    Me.txtgaddress2s.Text = Convert.ToString(dr("g_address2"))
                    Me.txtgaddress3s.Text = Convert.ToString(dr("g_address3"))
                    Me.txtgtelephonenos.Text = Convert.ToString(dr("g_telephoneNO"))
                End While
                dr.Close()
                cmd.Dispose()
                con.Close()

            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try

please help me guys :)

You aren't assigning a value to Query until after you create the cmd. Try

Query = "SELECT first_name, last_name, NIC_NO, c_address1,  " &
        "       c_address2, c_address3, c_telephoneNO,      " &
        "       membership_date, business_name, g_name,     " &
        "       g_nicno, g_address1, g_address2, g_address3," &
        "       g_telephoneNO                               " &
        "  FROM custormer                                   " &
        " WHERE custormer_id = '" & txtcustormerids.Text & "'"

con.Open()

Using cmd = New MySqlCommand(Query, con)
Member Avatar for සශික

no bro it doesn't go forward :/

Member Avatar for සශික

now my code look likes this, but had some errors :/

Try
            Dim con As New MySqlConnection
            'con.Open()
            'Query = "SELECT * FROM custormer WHERE custormer_id='" & Trim(txtcustormerids.Text) & "'"
            Query = "select first_name,last_name,NIC_NO,c_address1,c_address2,c_address3,c_telephoneNO,membership_date,business_name,g_name,g_nicno,g_address1,g_address2,g_address3,g_telephoneNO from custormer where custormer_id='" & Trim(txtcustormerids.Text) & "'"
            con.Open()
            dr = cmd.ExecuteReader()
            Using cmd = New MySqlCommand(Query, con)
                'con.Open()
                While dr2.Read()
                    Me.txtfirstnames.Text = Convert.ToString(dr("first_name"))
                    Me.txtlastnames.Text = Convert.ToString(dr("last_name"))
                    Me.txtnicnos.Text = Convert.ToString(dr("NIC_NO"))
                    Me.txtcaddress1s.Text = Convert.ToString(dr("c_address1"))
                    Me.txtcaddress2s.Text = Convert.ToString(dr("c_address2"))
                    Me.txtcaddress3s.Text = Convert.ToString(dr("c_address3"))
                    Me.txtctelephonenos.Text = Convert.ToString(dr("c_telephoneNO"))
                    Me.txtmembershipdates.Text = Convert.ToString(dr("membership_date"))
                    Me.txtbusinessnames.Text = Convert.ToString(dr("business_name"))
                    Me.txtgnames.Text = Convert.ToString(dr("g_name"))
                    Me.txtgnicnos.Text = Convert.ToString(dr("g_nicno"))
                    Me.txtgaddress1s.Text = Convert.ToString(dr("g_address1"))
                    Me.txtgaddress2s.Text = Convert.ToString(dr("g_address2"))
                    Me.txtgaddress3s.Text = Convert.ToString(dr("g_address3"))
                    Me.txtgtelephonenos.Text = Convert.ToString(dr("g_telephoneNO"))
                    cmd.ExecuteNonQuery()
                End While
                dr.Close()
                cmd.Dispose()
                con.Close()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try

maybe this could occured by my tabpage. my vb.net knowledge was small.
still I haven't any idea about where to open mysql connection and close....

You do

Dim con As New MySqlConnection

but you haven't assigned a connection string so there is no way to actually connect to your database. Also, you are doing ExecuteReader before you create cmd and assign the query string. For examples of how to connect, query and read please see this code snippet

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.