类型为' system.formatexception'的未处理异常发生在更新按钮中的mscorlib.dll中

时间:2015-11-16 17:17:15

标签: mysql vb.net

当我点击我的更新按钮时,我得到一个未处理的类型' System.FormatException'发生在mscorlib.dll中。我想更新我的数据库值。只有手机号码才能更新。

This is the error message

Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System
Imports System.Data

Public Class AddStudent
Dim connString As String = "server=localhost;userid=jared;password=jared;database=database;persistsecurityinfo=True"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim student As New DataTable()


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    Dim iReturn As Boolean
    Using SQLConnection As New MySqlConnection(connString)
        Using sqlCommand As New MySqlCommand()
            With sqlCommand
                .CommandText = "INSERT INTO students (StudentNumber, Firstname, Surname, Number) values (@sNumber,@sName,@sLname,@sNum)"
                .CommandType = CommandType.Text
                .Parameters.AddWithValue("@sNumber", TextBox1sn.Text)
                .Parameters.AddWithValue("@sName", TextBox1name.Text)
                .Parameters.AddWithValue("@sLname", TextBox2su.Text)
                .Parameters.AddWithValue("@sNum", TextBox3num.Text)

            End With
            Try
                SQLConnection.Open()
                sqlCommand.ExecuteNonQuery()
                iReturn = True
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
                iReturn = False



            Finally
                SQLConnection.Close()
            End Try
        End Using
    End Using
End Sub

Public Function InsertStud() As Boolean
    Dim iReturn As Boolean
    Using SQLConnection As New MySqlConnection(connString)
        Using sqlCommand As New MySqlCommand()
            With sqlCommand

                .CommandText = "INSERT INTO students (StudentNumber, Firstname, Surname, Number) values (@sNumber,@sName,@sLname,@sNum)"
                .Connection = SQLConnection
                .CommandType = CommandType.Text
                .Parameters.AddWithValue("@sNumber", TextBox1sn.Text)
                .Parameters.AddWithValue("@sName", TextBox1name.Text)
                .Parameters.AddWithValue("@sLname", TextBox2su.Text)
                .Parameters.AddWithValue("@sNum", TextBox3num.Text)


            End With
            Try
                SQLConnection.Open()
                sqlCommand.ExecuteNonQuery()
                iReturn = True
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
                iReturn = False


            Finally
                SQLConnection.Close()
            End Try
        End Using
    End Using
    Return iReturn
End Function

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click



    Dim iReturn As Boolean
    Using SQLConnection As New MySqlConnection(connString)
        Using sqlCommand As New MySqlCommand()
            With sqlCommand
                .CommandText = "UPDATE students SET StudentNumber = @sNumber, " & TextBox1sn.Text &
                   "`Name`= @sName, Surname=@sLname, Number= @sNum, " & _
                   "College= @sCollege, Course= " & _
                   ("Where StudentNumber = @oldNumber")
                .Connection = SQLConnection
                .Parameters.AddWithValue("@sNumber", SqlDbType.VarChar).Value = TextBox1sn.Text
                .Parameters.Add("@sName", MySqlDbType.VarChar).Value = TextBox1name.Text
                .Parameters.Add("@sLname", MySqlDbType.VarChar).Value = TextBox2su.Text
                .Parameters.Add("@sNum", MySqlDbType.VarChar).Value = TextBox3num.Text
                .Parameters.AddWithValue("@oldNumber", SqlDbType.VarChar).Value = TextBox1sn.Text


            End With
            Try
                SQLConnection.Open()
                sqlCommand.ExecuteNonQuery()
                iReturn = True
            Catch ex As MySqlException
                MsgBox(ex.Message.ToString)
                iReturn = False



            Finally
                SQLConnection.Close()
            End Try
        End Using
    End Using
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

错误的原因是在前4个参数上调用了AddWithValue。 AddWithValue第二个参数是您要分配给参数的值,而不是其类型。 使用type作为第二个参数意味着AddWithValue创建一个integer类型的参数,当您将该字符串赋值给Value属性时,您将获得异常

您应该更改所有内容以使用详细信息,但更安全的添加

With sqlCommand
    .CommandText = "UPDATE students SET StudentNumber = @sNumber, " & _ 
                   "`Name`= @sName, Surname=@sLname, Number= @sNum, " & _
                   "College= @sCollege, Course= @sCourse, Year = @sYr " & _
                   "Where StudentNumber = @oldNumber")
    .Connection = SQLConnection

    .Parameters.Add("@sNumber", SqlDbType.VarChar).Value = TextBox1sn.Text
    .Parameters.Add("@sName", MySqlDbType.VarChar).Value = TextBox1name.Text
    .Parameters.Add("@sLname", MySqlDbType.VarChar).Value = TextBox2su.Text
    .Parameters.Add("@sNum", MySqlDbType.VarChar).Value = TextBox3num.Text
    .Parameters.Add("@sCollege", ???).Value = ComboBox1.SelectedItem
    .Parameters.Add("@sCourse", ???).Value = ComboBox2.SelectedItem
    .Parameters.Add("@sYr", ???).Value = ComboBox3.SelectedItem
    .Parameters.Add("@oldNumber, SqlDbType.VarChar).Value = ???

当然有人已经告诉过你需要为查询的每个部分使用参数,(WHERE也需要一个参数)

另请注意,我已将传递给Add方法的枚举更改为使用特定的MySql。 SqlDbType是用于基类的通用枚举,我不确定它是否具有相同的MySql值,然后更好地使用匹配的值。

最后,最后三个参数使用相同的方法,但我不知道什么是预期的MySqlDbType,以及是否需要将组合的SelectedItem属性转换为适当的数据类型

修改
还存在Name是保留关键字的问题,将其置于反引号之间,并且不清楚您要更新的是什么。您在WHERE条件中使用Number。无需使用也用于搜索的文本框的值更新同一记录上的Number。相反,如果您尝试使用新值更改Number字段,则需要在WHERE条件中使用原始值。