InvalidOperationException未处理 - 轻松修复?

时间:2013-12-17 16:21:03

标签: mysql vb.net

嗨,有人可以帮助我,我是一个全新的编码,只是关注我老师给我的材料。

我目前正在制作一个连接到xampp-mysql数据库的vb程序

我已经创建了用户/传递存储在xampp数据库中的登录表单。我已经为文本框添加了一些状态检查,现在我正在尝试在我的程序中实现一个功能,用户可以使用他们的安全密码更改他/她的密码。

我的XAMPP数据库被称为:ba-solutions 我的表名为Login,我的字段是Username,Password和Security

我尝试在线查看,但没有任何意义或与我相关,但这可能只是因为我不理解它,因为我不熟悉编码。

我用老师的工作表为程序编写了这段代码,但是当我尝试运行时,我收到错误:

InvalidOperationException未处理 创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:初始化字符串的格式不符合从索引52开始的规范。

以下是我登录表单中的所有代码:

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

请记住,我是一个完整的新手,我真的很感激任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

我认为你的连接字符串无效

请改为尝试:

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