Jump to content

vb.net login thingy


Recommended Posts

Ok i'm teaching myself VB.Net now and atm I have 1 form with 2 text boxes labelled "Username" and "Password"

 

These are called UsernameTextBox and PasswordTextBox.

There is also 2 buttons.

One button is "Register" which I have working perfectly fine. It takes the username and password from the text boxes and adds them to a remote mysql database.

 

Now the other button is the login which is designed to login using info FROM mysql.

 

Imports MySql.Data.MySqlClient

Public Class LoginWindow

    Dim connectionString As String = "Database=DATABASENAME;Data Source=HOST;User Id=USERNAMEFORDATABASE;Password=DBPASSWORD;Connection Timeout=30"
    Dim connection As New MySqlConnection(connectionString)

    Private Sub LoginWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub RegisterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterButton.Click
        Dim InsertQry As String = "INSERT INTO Members (Username, Password) VALUES ('" + UsernameTextBox.Text + "','" + PasswordTextBox.Text + "')"
        Dim SendQry As New MySqlCommand(InsertQry, connection)
        Dim QryResult As Integer = 0

        connection.Open()
        QryResult = SendQry.ExecuteNonQuery()
        connection.Close()
        MsgBox("User Created! Now Login!")
        Try

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try


    End Sub

    Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
        Dim LoginQry As String = "SELECT Password FROM Members WHERE Username = " + UsernameTextBox.Text + ""
        Dim SendQry As New MySqlCommand(LoginQry, connection)
        Dim Result As DataTable
        Dim adapter As New MySqlDataAdapter

        connection.Open()

        SendQry.ExecuteNonQuery()
        adapter.SelectCommand = SendQry
        adapter.Fill(Result)

        connection.Close()

        If Result = PasswordTextBox.Text Then
            MsgBox("Login Successful")
        Else
            MsgBox("Login Failed")
        End If

    End Sub
End Class

 

That is the full code. Now I need the very last bit to work which is the compare password but I don't know how to check the single cell for the correct password needed. Also It returns a huge error If i type in a username that is not in the database and crashes the program

Unknown column 'john' in 'where clause'

 

 

Errors Returned:

 

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll

System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>LightSysMessenger.vshost.exe</AppDomain><Exception><ExceptionType>MySql.Data.MySqlClient.MySqlException, MySql.Data, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d</ExceptionType><Message>Unknown column 'john' in 'where clause'</Message><StackTrace>  at MySql.Data.MySqlClient.MySqlStream.ReadPacket()

  at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int32&amp; insertedId)

  at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp; affectedRows, Int32&amp; insertedId)

  at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)

  at MySql.Data.MySqlClient.MySqlDataReader.NextResult()

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()

  at LSMessenger.LoginWindow.LoginButton_Click(Object sender, EventArgs e) in C:\Users\Rayth\AppData\Local\Temporary Projects\WindowsApplication1\Login.vb:line 39

  at System.Windows.Forms.Control.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

  at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)

  at System.Windows.Forms.Control.WndProc(Message&amp; m)

  at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)

  at System.Windows.Forms.Button.WndProc(Message&amp; m)

  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)

  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)

  at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)

  at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

  at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

  at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

  at LSMessenger.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

  at System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'john' in 'where clause'

  at MySql.Data.MySqlClient.MySqlStream.ReadPacket()

  at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int32&amp; insertedId)

  at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32&amp; affectedRows, Int32&amp; insertedId)

  at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId)

  at MySql.Data.MySqlClient.MySqlDataReader.NextResult()

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()

  at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()

  at LSMessenger.LoginWindow.LoginButton_Click(Object sender, EventArgs e) in C:\Users\Rayth\AppData\Local\Temporary Projects\WindowsApplication1\Login.vb:line 39

  at System.Windows.Forms.Control.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

  at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)

  at System.Windows.Forms.Control.WndProc(Message&amp; m)

  at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)

  at System.Windows.Forms.Button.WndProc(Message&amp; m)

  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)

  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)

  at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)

  at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)

  at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)

  at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

  at LSMessenger.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

  at System.Threading.ThreadHelper.ThreadStart()</ExceptionString><DataItems><Data><Key>Server Error Code</Key><Value>1054</Value></Data></DataItems></Exception></TraceRecord>

Link to comment
https://forums.phpfreaks.com/topic/226642-vbnet-login-thingy/
Share on other sites

Try this

 

"SELECT [Password] FROM [Members] WHERE [username] = '" & UsernameTextBox.Text & "'" for vb.net

"SELECT [Password] FROM [Members] WHERE [username] = '" + UsernameTextBox.Text + "'" for C#

Notice the single qoutes before the double qoutes that should work

 

Also u should use Parameters . Better stuctured code and easy to find errors

 

You have the chance of SQL Injection

Link to comment
https://forums.phpfreaks.com/topic/226642-vbnet-login-thingy/#findComment-1171964
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.