Livaren
Livaren

Reputation: 41

InvalidOperationException was unhandled - Easy fix?

Hi could someone please help me, I am a completely new to coding and just following material my teacher has given me.

I am currently making a vb programme connected to xampp-mysql database

I have made the login form where the user/pass is store on the xampp database. I have included some presence checks for the text boxes and now I am trying to implement a feature into my program where the user can change his/her password using their security passphrase.

My XAMPP database is called: ba-solutions My table is called Login and my fields are Username, Password and Security

I tried looking online, but nothing makes sense or is relevant to me, but this maybe just because I don't understand it as I am new to coding.

I used the sheets from my teacher to write this code for the programme but when I try to run I get the error:

InvalidOperationException was unhandled An error occurred creating the form. See Exception.InnerException for details. The error is: Format of the initialization string does not conform to specification starting at index 52.

Here is my all my code from my Login form:

Imports MySql.Data
Imports MySql.Data.MySqlClient
Module procedures_and_variables

Public objconnection As New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=")

Public objdataadapter As New MySqlDataAdapter
Public objdataset As DataSet
Public objcommandbuilder As New MySqlCommandBuilder
Public objdatatable As New DataTable
Public rowposition As Integer = 0
Public sqlstring As String
Public tablename As String
Public objcommand As MySqlCommand
Public reader As MySqlDataReader
Public database_path As String = "Server=localhost;database=ba-solutions;user id=root;password="
Public path As String
Public backup As New MySqlBackup

'Procedure which checks whether or not the current connection is open and opens it, if it is closed.
Public Sub connection_checker()
    If objconnection.State = ConnectionState.Closed Then
        Try
            objconnection.Open()
        Catch ex As MySqlException
            MsgBox("Error connecting to database")
        End Try
    End If
End Sub

'Procedure which executes any SQL query.
Public Sub SQL_executer()
    Call connection_checker()

    objdataadapter.SelectCommand = New MySqlCommand
    objdataadapter.SelectCommand.Connection = objconnection
    objdataadapter.SelectCommand.CommandText = sqlstring

    objcommandbuilder = New MySqlCommandBuilder(objdataadapter)
    objdataadapter.Fill(objdatatable)
    objdataadapter.SelectCommand.CommandType = CommandType.Text

End Sub

'Procedure used to load data from the database for the selected table.
Public Sub initial_load()
    Call connection_checker()
    Call SQL_executer()

    objdataset = New DataSet
    objdataadapter.Fill(objdataset, tablename)
    objconnection.Close()

End Sub

'Procedure used to update data in a table with the changes made to the data in the datagrid.
Public Sub update_data()
    Call connection_checker()
    Try
        objdataadapter.Update(objdataset, tablename)
        MsgBox("Changes accepted", MsgBoxStyle.Information, "Update successfull")
    Catch ex As Exception
        MsgBox("Changes declined", MsgBoxStyle.Critical, "Update unsuccessfull")

    End Try
End Sub

'Procedures used to bind the relevant data to the data grid, with the correct header titles.
Public Sub bind_dataset_client_details()
    'NEEDS TO BE COMPLETED FOR ALL DATASETS
End Sub
End module

Public Class frmLogin
    Dim form_type As Form
    Dim user_table As String
    Dim objconnection As New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password;")
    Dim sqlstring As String

Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    objconnection.Open()
    objdataadapter.SelectCommand = New MySqlCommand
    objdataadapter.SelectCommand.Connection = objconnection
    objdataadapter.SelectCommand.CommandText = "Select * FROM Login"
End Sub

Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click
    Me.Cursor = Cursors.WaitCursor

    'Tries to open the server connection and it will give an error if connection fails.
    Try
        objconnection.Open()
    Catch ex As Exception
        MsgBox("Error connecting to database", MsgBoxStyle.Critical, "Database Error")
        frmXampp.Show()
    End Try

    'Checks if the username textbox contains a value, if it does not, it creates an error provider.
    If Len(txtUsername.Text) < 1 Then
        MsgBox("You must enter a username.", MsgBoxStyle.OkOnly, "Login Error")
    End If
    'Performs same presence check as above on password textbox.
    If Len(txtPassword.Text) < 1 Then
        MsgBox("You must enter a password.", MsgBoxStyle.OkOnly, "Login Error")
    End If

    'SQL query
    sqlstring = "SELECT * FROM Login Where username = '" + txtUsername.Text + "' AND password = '" + txtPassword.Text + "'"
    'Creates command
    objcommand = New MySqlCommand(sqlstring, objconnection)
    'Executes command
    reader = objcommand.ExecuteReader

    'See if user exists
    If reader.Read Then
        MsgBox("Login Accepted!", MsgBoxStyle.Information, "Login Successful")
        'Displays the Main Menu form after a successfull login
        frmMainMenu.Show()
        Me.Visible = False
    Else
        'And if authentication has failed, the user will be given an option to retry or exit
        reader.Close()
        Dim prompt As MsgBoxResult
        prompt = MessageBox.Show("Invalid Username or Password. Please make sure your credentials are correct and try again or exit.", "Login Error",
    MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
        If prompt = MsgBoxResult.Cancel Then
            Me.Close()
        End If
    End If
    Me.Cursor = Cursors.Arrow
End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    Me.Close()
End Sub

Private Sub ForgotPassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ForgotPassword.Click
    Dim security, newpassword As String
    security = InputBox("What is the security passphrase?")
    sqlstring = "SELECT security FROM Login WHERE security =  '" & security & "'"
    objcommand = New MySqlCommand(sqlstring, objconnection)
    reader = objcommand.ExecuteReader

    If reader.Read Then
        reader.Close()
        newpassword = InputBox("Enter new password")
        sqlstring = "UPDATE 'Login' SET 'password' = '" & newpassword &
            "' WHERE 'Login' . 'password' = '" & security & "'"
        objdataadapter.SelectCommand.CommandText = sqlstring
        objdataadapter.SelectCommand.CommandType = CommandType.Text

        objdataset = New DataSet
        objdataadapter.Fill(objdataset, "Login")

        objconnection.Close()

    Else
        MsgBox("Invalid Security Passphrase or New Password. Please make sure your credentials are correct and try again.", MsgBoxStyle.Critical, "Authentication Failed")
        reader.Close()
    End If
End Sub

End Class

Please bear in mind I am a complete newbie and I would really appreciate any help. Thank you.

Upvotes: 1

Views: 701

Answers (1)

ɐsɹǝʌ ǝɔıʌ
ɐsɹǝʌ ǝɔıʌ

Reputation: 4512

I think you have an invalid connection string

Try this instead:

Server=localhost;Database=ba-solutions;Uid=root;

Upvotes: 1

Related Questions