VB使用MySql登录网站

时间:2014-11-27 01:52:41

标签: mysql vb.net

我正在学习VB并尝试创建一个网站。它需要登录。所以我创建了一个登录表单,在VB文件中我编写了以下代码。

          Imports System.Data.Odbc
          Partial Public Class login_2
          Inherits System.Web.UI.Page


Shared login_username As Object
Shared login_password As Object

Protected Sub LogIn_Clicked(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
    Dim cn As New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=127.0.0.1;Database=hotel_test; User=root;Password=Amir1234;")
    cn.Open()
    Dim cmd As New OdbcCommand("Select * from tbl_login where username=? and password=?", cn)

    'Add parameters to get the username and password  

    cmd.Parameters.Add("@user_name", OdbcType.VarChar)
    cmd.Parameters("@user_name").Value = login_2.login_username.ToString

    cmd.Parameters.Add("@password", OdbcType.VarChar)
    cmd.Parameters("@password").Value = login_2.login_password.ToString

    Dim dr As OdbcDataReader
    ' Initialise a reader to read the rows from the login table.  
    ' If row exists, the login is successful  

    dr = cmd.ExecuteReader



End Sub

End Class

我正在使用Visual Studio,当我尝试运行该表单时,它会显示以下错误。

        Server Error in '/' Application.
    Unable to cast object of type 'System.EventArgs' to type                            'System.Web.UI.WebControls.AuthenticateEventArgs'.          
  Description: An unhandled exception occurred during the execution of the current web    request. Please review the stack trace for more information about the error and       where it originated in the code.

 Exception Details: System.InvalidCastException: Unable to cast object of type           'System.EventArgs' to type 'System.Web.UI.WebControls.AuthenticateEventArgs'.

来源错误:

    <div class="submit">
    <asp:Button Text="Log in" runat="server" OnClick="LogIn_Clicked" />

寻求帮助。

1 个答案:

答案 0 :(得分:0)

问题是您的HTML引用了asp:button,但后面的代码似乎是asp:login OnAuthenticate请求的剩余代码。

如果您不再使用asp:login控件,那么简单的解决方案是修复事件签名:

Protected Sub LogIn_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)

另一方面,如果你真的打算使用登录控件,那么你需要修复HTML

<asp:Button Text="Log in" runat="server" OnClick="LogIn_Clicked" />

类似于:

<asp:Login id="Login1" runat="server" OnAuthenticate="LogIn_Clicked" />

有关登录控件的详细信息,请参阅MSDN documentation