1

帮助!我正在制作一个将托管在一个简单网站上的 Web 应用程序。它只是一个简单的登录应用程序,在它自己的页面上。它所要做的就是在给出正确的凭据时登录(我有一个带有用户名和密码的预制数据库)。我在 vb .aspx 文件中执行此操作,但无法弄清楚(我不是 vb 专家)。

我在运行页面时遇到的错误是这个编译错误:

编译器错误消息:BC30469:对非共享成员的引用需要对象引用。 错误发生在第 19 行:cmd.Parameters.AddWithValue("@username", txtUserName.Text)

这是我在 default.aspx 文件中的代码:

<%@ Page Language="VB" %>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>


<!DOCTYPE html>
<script runat="server">


    Partial Class _Default
        Inherits System.Web.UI.Page
        Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        End Sub
        Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
            con.Open()
            Dim cmd As New SqlCommand("select * from users where UserName =@username and Password=@password", con)
            cmd.Parameters.AddWithValue("@username", txtUserName.Text)
            cmd.Parameters.AddWithValue("@password", txtPWD.Text)
            Dim da As New SqlDataAdapter(cmd)
            Dim dt As New DataTable()
            da.Fill(dt)
            If dt.Rows.Count > 0 Then
                Response.Redirect("welcome.html")
            Else
                ClientScript.RegisterStartupScript(Page.[GetType](), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>")
            End If
        End Sub
    End Class
</script>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>

        <link rel="stylesheet" type="text/css" href="stylesheet2.css">
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table>
                    <tr>
                        <td>Username:
                        </td>
                        <td>
                            <asp:TextBox ID="txtUserName" runat="server" OnTextChanged="txtUserName_TextChanged" />
                            <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>Password:
                        </td>
                        <td>
                            <asp:TextBox ID="txtPWD" runat="server" TextMode="Password" />
                            <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
</html>
4

1 回答 1

1

请删除以下行:

Partial Class _Default
        Inherits System.Web.UI.Page

End Class

编写内联代码时,不应出现此问题。

此外,您可能需要更正该Else声明。

Dim cstext1 As String = "<script language='javascript'>alert('Invalid Username and Password')</"+"script>"

现在把这个变量: cstext1 里面RegisterStartupScript

ClientScript.RegisterStartupScript(Page.[GetType](), "validation", cstext1)
于 2013-07-30T09:45:58.337 回答