Single Result from SQL Database

Works in MySQL Query Browser - not in VB

Page 1 of 1

5 Replies - 2102 Views - Last Post: 19 June 2009 - 11:44 PM Rate Topic: -----

#1 SpeeDemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 18-March 08

Single Result from SQL Database

Posted 19 June 2009 - 11:23 PM

I shall start off by stating that I am a beginner with SQL and VB.NET - yay!

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 Class
Thank you for reading this far!

Is This A Good Question/Topic? 0
  • +

Replies To: Single Result from SQL Database

#2 MattjCook07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 16-June 09

Re: Single Result from SQL Database

Posted 19 June 2009 - 11:28 PM

View PostSpeeDemon, on 19 Jun, 2009 - 10:23 PM, said:

I shall start off by stating that I am a beginner with SQL and VB.NET - yay!

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 Class
Thank you for reading this far!


I can help you, if your still online reply here and I we can meet on a messenger. You need to had a variable to your command
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,289
  • Joined: 12-March 09

Re: Single Result from SQL Database

Posted 19 June 2009 - 11:31 PM

change your this code
myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = " & " ' txtUsername.Text ' "

to
myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = '" & txtUsername.Text & "'"


hope it help you :)
Was This Post Helpful? 0
  • +
  • -

#4 MattjCook07  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 16-June 09

Re: Single Result from SQL Database

Posted 19 June 2009 - 11:33 PM

View PostMattjCook07, on 19 Jun, 2009 - 10:28 PM, said:

View PostSpeeDemon, on 19 Jun, 2009 - 10:23 PM, said:

I shall start off by stating that I am a beginner with SQL and VB.NET - yay!

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 Class
Thank you for reading this far!


I can help you, if your still online reply here and I we can meet on a messenger. You need to had a variable to your command



well not sure if you are still around, but you should use

mycommand.commandtype and set that to something to the effect of 'sql query'
Was This Post Helpful? 1
  • +
  • -

#5 SpeeDemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 18-March 08

Re: Single Result from SQL Database

Posted 19 June 2009 - 11:39 PM

View Postnoorahmad, on 19 Jun, 2009 - 10:31 PM, said:

change your this code
myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = " & " ' txtUsername.Text ' "

to
myCommand.CommandText = "SELECT USER_ID FROM aaalogin where NAME = '" & txtUsername.Text & "'"


hope it help you :)



BAM! WORKS! TYVM!

I spent about an HOUR on this, hahaha. Its been far too long since I have programmed anything to remember how to jam in this double quote business.
Was This Post Helpful? 0
  • +
  • -

#6 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,289
  • Joined: 12-March 09

Re: Single Result from SQL Database

Posted 19 June 2009 - 11:44 PM

Quote

BAM! WORKS! TYVM!

Your Welcome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1