Registration

Unable to fix eror

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2154 Views - Last Post: 10 April 2009 - 12:40 PM Rate Topic: -----

#1 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Registration

Posted 09 April 2009 - 03:00 PM

The following is the error i get for my registration program. I know the error is caused by the encryption part. I just don't know how to fix it. It also doesnt insert into my database.

Operator '+' is not defined for type 'Strings' and '1-dimensional array of Byte.' ALong with the following when i change the value back to PasswordTextBox

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''userlist' (UserName, Password, Email) Value ('test','test','test@test.com','Use' at line 1

Here is my code

Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Security.Cryptography
Imports System.Text

Public Class registration
	Const AppName = "Gameapp MySQL Login System"


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		If UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Then
			MessageBox.Show("Please enter the required information to login.", AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)
		Else
			Dim mMySQLConnectionString As String = (My.Settings.myServer & My.Settings.myDB & My.Settings.myUsername & My.Settings.myPassword)
			' You will need to change the connection string above to yours
			Debug.WriteLine(mMySQLConnectionString)
			Dim conn As MySqlConnection
			Dim cmd As New MySqlCommand
			conn = New MySqlConnection()
			conn.ConnectionString = mMySQLConnectionString

			Dim strText As String = PasswordTextBox.Text
			Dim bytHashedData As Byte()
			Dim encoder As New UTF8Encoding()
			Dim md5Hasher As New MD5CryptoServiceProvider
			bytHashedData = md5Hasher.ComputeHash(encoder.GetBytes(strText))

			Try
				conn.Open()
			Catch myerror As MySqlException
				MsgBox("Error Connecting to Database")
			End Try
			'sql query
			Dim myAdapter As New MySqlDataAdapter

			Dim Sqlquery = "Select UserName From userlist WHERE UserName = '" + UsernameTextBox.Text + "'"
			Dim myCommand As New MySqlCommand()
			myCommand.Connection = conn
			myCommand.CommandText = Sqlquery
			'Start query
			myAdapter.SelectCommand = myCommand
			Dim myData As MySqlDataReader
			myData = myCommand.ExecuteReader()

			'check to see if user exsits
			If myData.HasRows = 0 Then
				MsgBox("You are now Registerd.")
				conn.Close()
				conn.Open()
				Dim registerfinal As New MySqlDataAdapter

				Dim query = "INSERT INTO userlist (UserName, Password, Email) Value ('" + UsernameTextBox.Text + "','" + bytHashedData + "','" + EmailTextBox.Text + "','" + Label1.Text + "')"
				Dim Command As New MySqlCommand()
				myCommand.Connection = conn
				myCommand.CommandText = query
				'start query
				myAdapter.SelectCommand = myCommand

				myData = myCommand.ExecuteReader()

			Else
				MsgBox("Username already in use.")

			End If


		End If
	End Sub

This post has been edited by crzyone9584: 09 April 2009 - 03:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Registration

#2 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Registration

Posted 09 April 2009 - 03:12 PM

Use ampersand (&) instead of + to concatenate strings.

This post has been edited by June7: 09 April 2009 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 03:55 PM

View PostJune7, on 9 Apr, 2009 - 02:12 PM, said:

Use ampersand (&) instead of + to concatenate strings.


same error as before

Operator '&' is not defined for type 'Strings' and '1-dimensional array of Byte.'
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:03 PM

Plus the array of bytes, you use for the hash value should be convert to a type the database understands.
Change
Dim Sqlquery = "Select UserName From userlist WHERE UserName = '" + UsernameTextBox.Text + "'"
 

to
Dim Sqlquery As String = "Select UserName From userlist WHERE UserName = '" & UsernameTextBox.Text & "'"


This post has been edited by AdamSpeight2008: 09 April 2009 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 04:06 PM

View PostAdamSpeight2008, on 9 Apr, 2009 - 03:03 PM, said:

Plus the array of bytes, you use for the hash value should be convert to a type the database understands.



it worked before. i lost my project and code do to HD failure. I never saw the failure coming. I just dont know how i got it to work before.

This post has been edited by crzyone9584: 09 April 2009 - 04:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:16 PM

Could it have been treating it as string?
Was This Post Helpful? 0
  • +
  • -

#7 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 04:20 PM

View PostAdamSpeight2008, on 9 Apr, 2009 - 03:03 PM, said:

Plus the array of bytes, you use for the hash value should be convert to a type the database understands.
Change
Dim Sqlquery = "Select UserName From userlist WHERE UserName = '" + UsernameTextBox.Text + "'"
 

to
Dim Sqlquery As String = "Select UserName From userlist WHERE UserName = '" & UsernameTextBox.Text & "'"




thats not wehre the problem is is in the INSERT query
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:24 PM

Do the same for the insert query.
Was This Post Helpful? 0
  • +
  • -

#9 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 04:26 PM

View PostAdamSpeight2008, on 9 Apr, 2009 - 03:24 PM, said:

Do the same for the insert query.



as stated a few posts up when suggested i tried and got this

Operator '&' is not defined for type 'Strings' and '1-dimensional array of Byte.'
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:30 PM

You're adding an array of bytes (the hashcode) to a string
Was This Post Helpful? 0
  • +
  • -

#11 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 04:31 PM

ok how would i fix it?
Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:33 PM

Convert it to a base64 string. (In this case).

This post has been edited by AdamSpeight2008: 09 April 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 04:37 PM

View PostAdamSpeight2008, on 9 Apr, 2009 - 03:33 PM, said:

Convert it to a base64 string. (In this case).


so convert the string to base 64 string and it should work?

Also i have another error i edited the first post about.
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Registration

Posted 09 April 2009 - 04:46 PM

Operator '+' is not defined for type 'Strings' and '1-dimensional array of Byte.'

(Variable of Type String) + (Variable of an array of type byte)

vb.net does know how to perform addition consisting of an array of bytes and string.

The error message is very self explanatory.

General: Don't edit your original post as it is make the thread hard to followm for future readers.

Instead add your update code in a new reply, now reader for follow the chain of thought.
Was This Post Helpful? 0
  • +
  • -

#15 crzyone9584  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 201
  • Joined: 22-April 08

Re: Registration

Posted 09 April 2009 - 08:09 PM

Ok. So i re written the registration code using some things from my login form. i fixed the errors that gave me. Now when i click the ok button i get this

Error: Connection must be open and valid

All my connections strings are correct do to i still able to connect from the login form. The Connection string is a direct copy from my login form.

Here is my updated code

Public Class registration
	Const AppName = "Gameapp MySQL Login System"


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mMySQLConnectionString As String = (My.Settings.myServer & My.Settings.myDB & My.Settings.myUsername & My.Settings.myPassword)
		' You will need to change the connection string above to yours
		Debug.WriteLine(mMySQLConnectionString)
		Dim conn As MySqlConnection
		Dim cmd As New MySqlCommand
		Dim dr As MySqlDataReader

		conn = New MySqlConnection()
		conn.ConnectionString = mMySQLConnectionString

		Dim strText As String = PasswordTextBox.Text
		Dim bytHashedData As Byte()
		Dim encoder As New UTF8Encoding()
		Dim md5Hasher As New MD5CryptoServiceProvider
		bytHashedData = md5Hasher.ComputeHash(encoder.GetBytes(strText))

		Try
			conn.Open()
			Dim myAdapter As New MySqlDataAdapter

			Dim Sqlquery = "Select UserName From userlist WHERE UserName = '" + UsernameTextBox.Text + "'"
			Dim myCommand As New MySqlCommand()
			myCommand.Connection = conn
			myCommand.CommandText = Sqlquery
			'Start query
			myAdapter.SelectCommand = myCommand

			dr = cmd.ExecuteReader

			If dr.HasRows = 0 Then
				conn.Close()
				conn.Open()
				MsgBox("Account Created")
				cmd.CommandText = "INSERT INTO userlist (UserName, Password, EmailAddress, Status) VALUES (?UserName, ?Password, ?EmailAddress, ?Status)"
				cmd.Parameters.Add(New MySqlParameter("?UserName", UsernameTextBox.Text))
				cmd.Parameters.Add(New MySqlParameter("?Password", bytHashedData))
				cmd.Parameters.Add(New MySqlParameter("?EmailAddress", EmailTextBox.Text))
				cmd.Parameters.Add(New MySqlParameter("?Status", Label4.Text))

				cmd.Connection = conn
				dr = cmd.ExecuteReader

			Else
				MessageBox.Show("Username is already in use.")
				PasswordTextBox.ResetText()
				UsernameTextBox.ResetText()
				UsernameTextBox.Focus()

			End If

		Catch myerror As MySqlException
			MessageBox.Show("Database Error: " & myerror.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)

		Catch ex As Exception

			MessageBox.Show("Error: " & ex.Message, AppName, MessageBoxButtons.OK, MessageBoxIcon.Information)

		Finally


			cmd.Parameters.Clear()
			conn.Close()

		End Try

	End Sub

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2