简体   繁体   English

对象引用未设置为对象的实例

[英]Object reference isn't set to an instance of an object

The error is in this image: 错误在此图像中:

错误信息

How do I fix this? 我该如何解决?

My code: 我的代码:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim con As New MySqlConnection("host=localhost; username=root; database= login")
    Dim cmd As MySqlCommand
    Dim dr As MySqlDataReader

    con.Open()
    cmd.Connection = con 'this is where I have an error

    cmd.CommandText = "select userID, password from login where userID= ' " & userIDtxt.Text & " ' and password = ' " & pwTxt.Text & " '"
    dr = cmd.ExecuteReader

    If dr.HasRows Then
        MsgBox("Login successful!")
        Me.Hide()
        MainForm.Show()
    Else
        MsgBox("Username or password does not match")
        userIDtxt.Text = ""
        pwTxt.Text = " "
    End If

End Sub
End Class

your sql connection string missing true credentials, this sample to connect to mysql with vb and c#: 您的sql连接字符串缺少真实凭据,此示例使用vb和c#连接到mysql:

https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html

You haven't initialized a new instance of your cmd variable. 您尚未初始化cmd变量的新实例。

Change: 更改:

Dim cmd As MySqlCommand

to: 至:

Dim cmd As New MySqlCommand

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

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