This the code i have written, at one point it displayed System.Data.Dataset return in Access level but it wasn't what i was looking for. Username is a string, Password is also a string and Access level is an integer. is there an alternative method of doing this that is more efficient? Also could you explain why i am getting the fault so i don't run into similar things in the future. Thanks in advance
[A picture of the error and what access prints][1]
Public Class Login
Dim Access_level As Integer
Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Parts.User' table. You can move, or remove it, as needed.
'Me.UserTableAdapter.Fill(Me.Parts.User)
End Sub
Public Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("Oops ¯\_(ツ)_/¯ " + Err.Description(), MsgBoxStyle.OkOnly, "Enter Value")
Else
Try
Await getDataSet(TextBox1.Text, TextBox2.Text, Access_level)
Print(username)
Print(password)
If username = TextBox1.Text And password = TextBox2.Text Then
Dim Access As Integer = Access_level
Print(Access)
If Access = 1 Then
Me.Hide()
AdminMainMenu.Show()
Else
Me.Hide()
MainMenu.Show()
End If
End If
Catch ex As Exception
'MsgBox("Oops " + Err.Description(), MsgBoxStyle.OkOnly, "Failed to Open")
'MsgBox("Incorrect login details", MsgBoxStyle.OkOnly)
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Public Async Function getDataSet(username As String, password As String, Access_level As Integer) As Task(Of DataSet)
Return Await Task.Factory.StartNew(
Function()
Dim connectionString = "server=localhost; userid=root; password=; database=partstest1; CharSet=utf8;"
Dim commandText = "SELECT Username, Password, Accesslevel FROM `user` WHERE `Username` = '" & username & "' and `Password` = '" & SHA256(password) & "';"
Using connDB = New MySqlConnection(connectionString), objCmd = New MySqlCommand(), objAdpt = New MySqlDataAdapter()
connDB.Open()
objCmd.Connection = connDB
objCmd.CommandText = commandText
objCmd.CommandType = CommandType.Text
objAdpt.SelectCommand = objCmd
Dim objDs = New DataSet()
objAdpt.Fill(objDs)
Console.WriteLine(objDs)
Return objDs
End Using
End Function)
End Function
[1]: https://i.stack.imgur.com/g9CIj.png
You shouldn't need all the Async stuff to retrieve one piece of data.
The Datbase code:
You can pass the .CommandText and the the command connection directly to the constructor of the command.
You already have the user name and password. Don't retrieve data you don't need.
Always use parameters to avoid Sql injection and make sql statements easier to write.
I assume Sha256(password) is a custon Function.
CommandType.Text is the defalut and does not have to be explicity assigned.
You do not need a DataAdapter. You are not going to update this data here. Anyway your adapter falls out of scope so it is of no value.
You do not need a DataSet. You are only dealing with one piece of data, not multiple tables. Use .ExecuteScalar to return the first column of the first row of the result set.
EDIT
The button code...
Changed retVal to Object.
Removed CInt from objCmd.ExecuteScalar()
Checked for Nothing and returned -1 Else CInt(RetVal)