简体   繁体   English

vb.net上的FormatException是未处理的错误

[英]FormatException was unhandled error on vb.net

I'm new to Stack Overflow, but anyway I'm having problem regarding my code. 我是Stack Overflow的新手,但是无论如何我在代码方面都遇到了问题。 When I try to run the program and click the Login function I am getting an error on this statement ... Adapter.Fill(Table) It says: FormatException was unhandled. 当我尝试运行该程序并单击Login函数时,在此语句上出现错误... Adapter.Fill(Table)它说:未处理FormatException。 An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format. mscorlib.dll中发生了'System.FormatException'类型的未处理异常。其他信息:输入字符串的格式不正确。

Imports MySql.Data.MySqlClient
Public Class Login_page
    Private Sub Login_page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
    End Sub

    Private Sub PictureBox3_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click
        Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim MysqlConnection As New MySqlConnection("host=127.0.0.1; user=root; database = storage_db")
        Dim Command As New MySqlCommand("SELECT * FROM `login_access` where username = @username and password = @password", MysqlConnection)

        Command.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = TextBoxUsername.Text
        Command.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = TextBoxPassword.Text

        Dim Adapter As New MySqlDataAdapter(Command)
        Dim Table As New DataTable()

        Adapter.Fill(Table)

        If Table.Rows.Count() <= 0 Then
            MessageBox.Show("Invalid")
        Else
            MessageBox.Show("Login Successfully")
        End If

    End Sub
End Class

These lines are wrong for a number of reasons: 这些行是错误的,原因有很多:

Command.Parameters.AddWithValue("@username", SqlDbType.VarChar).Value = TextBoxUsername.Text
Command.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = TextBoxPassword.Text

Firstly, you're using MySqlClient for MySQL, not SqlClient for SQL Server. 首先,您使用的是MySQL的MySqlClient ,而不是SQL Server的SqlClient That means that you need to use MySqlDbType and not SqlDbType . 这意味着您需要使用MySqlDbType而不是SqlDbType

Secondly and thirdly, you need to decide whether you're using Add or AddWithValue because, as it is, you're mixing and matching. 其次,第三,您需要确定是要使用Add还是AddWithValue ,因为实际上是在混合和匹配。 If you use AddWithValue then you don't specify the data type as it is inferred from the value: 如果使用AddWithValue ,则不指定数据类型,因为它是从值推断的:

Command.Parameters.AddWithValue("@username", TextBoxUsername.Text)
Command.Parameters.AddWithValue("@password", TextBoxPassword.Text)

That's generally discouraged though, because the inferred type is often not the type you want. 但是通常不建议这样做,因为推断的类型通常不是您想要的类型。 That means that you should use Add . 这意味着您应该使用Add In that case, you should also specify the size of variable-length data types: 在这种情况下,您还应该指定可变长度数据类型的大小:

Command.Parameters.Add("@username", MySqlDbType.VarChar, 50).Value = TextBoxUsername.Text
Command.Parameters.Add("@password", MySqlDbType.VarChar, 50).Value = TextBoxPassword.Text

EDIT: 编辑:

For the record, what was happening in your original code was basically this: 为了记录,您的原始代码中发生的基本上是这样的:

Dim p = Command.Parameters.AddWithValue("@username", SqlDbType.VarChar)

p.Value = TextBoxUsername.Text

So a parameter was being added with a value of SqlDbType.VarChar , which would have been interpreted as an Integer and the parameter data type inferred from that. 因此,正在添加一个带有SqlDbType.VarChar值的参数,该值将被解释为Integer并由此推断出参数数据类型。 The Value of that parameter was then being set using a String that could not be converted to an Integer , thus the FormatException was thrown. 然后使用无法将其转换为IntegerString设置该参数的Value ,因此引发了FormatException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM