0

i have a code but it's not working. i was trying to put a value in label2 but it's not working. please help me.

Private Sub student_no_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles student_no.Click
    MySqlConnection = New MySqlConnection
    MySqlConnection.ConnectionString = "server = localhost; port=3307; user id = root; password = 1234; database = sample;"
    Dim READER As MySqlDataReader

    Try
        MySqlConnection.Open()
        Dim query As String
        query = " select id from sample.student where last_name = '" & txtlastname.Text & "' "
        Dim Command As New MySqlCommand(query, MySqlConnection)
        READER = Command.ExecuteReader
        Label2.Text = query.ToString

        MessageBox.Show("Student Number Generated")
        MySqlConnection.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConnection.Dispose()

    End Try

End Sub

2 Answers 2

1

You are using .ToString on query, which is a string. What you should be doing is operations on the READER object.

Since SELECT will always return a list of results, you have to treat the results as such, like...

While READER.Read()
  MessageBox.Show((READER.GetInt32(0)))
End While

.Read() returns the next element in the returned rowset

Sign up to request clarification or add additional context in comments.

Comments

0
If READER.Read() Then
    Label2.Text = READER.GetString(0)
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.