I am trying to read a single result from an SQL table. I tested my query in MySQL Query Browser and it returns a result of USER_ID=4. My query:
SELECT USER_ID FROM aaalogin where NAME = 'administrator'I believe my error is due to some "double quoting". I have a "txtUsername" textbox on my login form. I want to inject the typed entry from this textbox into my SQL QUERY. Notice the end of this line, I have tried tons of combinations, nothing works.
myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = " & " ' txtUsername.Text ' "The result is ALWAYS "0" though, which is useless to me. Full code below:
Imports MySql.Data.MySqlClient Imports System.Data Public Class frmLogin Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click Application.Exit() End Sub Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click Dim conn As MySqlConnection Dim myConnString As String Dim myCommand As New MySqlCommand Dim UserID As Integer myConnString = "server=localhost;user id=root;database=servicedesk;port=33366" conn = New MySqlConnection() conn.ConnectionString = myConnString Try conn.Open() myCommand.Connection = conn myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = " & " ' txtUsername.Text ' " UserID = myCommand.ExecuteScalar MessageBox.Show(UserID) conn.Close() Dim mainForm As New frmMain mainForm.UserID = UserID mainForm.connectionString = myConnString mainForm.Show() Me.Hide() Me.Close() Catch myerror As MySqlException MessageBox.Show("Error Connecting to Database: " & myerror.Message) Finally conn.Dispose() End Try End Sub End ClassThank you for reading this far!