输入了正确的数据,但登录使 c# asp.net 失败

本文关键字:登录 asp 失败 net 数据 输入 | 更新日期: 2023-09-27 18:32:22

我是 c# 的新手,asp.net.从教程中获取了此代码,但我收到错误。我的标签显示登录失败,尽管我输入了正确的数据。请帮我:(它说:当我输入不正确的数据时,对象引用未设置为对象的实例。

这是我的连接类.cs

public class ConnectionClass
 {
 private static SqlConnection conn;
 private static SqlCommand command;
static ConnectionClass()
{
    string connectionString = ConfigurationManager.ConnectionStrings["CoffeeDBConnectionString"].ToString();
    conn = new SqlConnection(connectionString);
    command = new SqlCommand("", conn);
}
public static User LoginUser(string login, string password)
{ 
    //Check if user exists
    string query = string.Format("SELECT COUNT(*) FROM SkyMusic.dbo.user WHERE username = '{0}'", login);
    command.CommandText = query;
    try
    {
        conn.Open();
        int numofUsers = (int) command.ExecuteScalar();
        if (numofUsers < 1)
        {
            //user exists, check if the passwords match
            query = string.Format("SELECT password FROM user WHERE username = '{0}'", login);
            command.CommandText = query;
            string dbPassword = command.ExecuteScalar().ToString();
            if (dbPassword == password)
            {
                //Passwords match
                query = string.Format("SELECT email, user_type FROM users WHERE username = '{0}'", login);
                command.CommandText = query;
                SqlDataReader reader = command.ExecuteReader();
                User user = null;
                while (reader.Read())
                {
                    string email = reader.GetString(0);
                    string type = reader.GetString(1);
                    user = new User(login, password, email, type);
                }
                return user;
            }
            else
            {
                //passwords do not match
                return null;
            }
        }
        else
        {
            return null;
        }
    }
    finally
    {
        conn.Close();
    }
}

我的登录名:

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="formcont">
<table>
<tr>
<td><asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label></td>
<td><asp:TextBox ID="TextBox1" runat="server" Width="142px"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label2" runat="server" Text="Password:"></asp:Label></td>
<td><asp:TextBox ID="TextBox2" runat="server" Width="142px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2"></br><asp:Button ID="Button1" runat="server" Text="Log In" />
    <br />
    </td>
</tr>
</table>
<p>Don't have an account? Click <a href="Register.aspx" runat="server">here</a>!</p>
</div>
</asp:Content>

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        User user = ConnectionClass.LoginUser(txtUname.Text, txtPass.Text);

        if (user != null)
        {
            Session["login"] = user.login;
            Session["type"] = user.type;
            Response.Redirect("Home.aspx");
        }
        else
        {
            lblError.Text = "Login Failed";
        }
     }

请帮忙:(这是我的数据库ID、用户名、密码、电子邮件、user_type

输入了正确的数据,但登录使 c# asp.net 失败

有两个问题或错误
1. 您无法使用正确的凭据登录:
检查这个。

if (numofUsers < 1)
    {
        //user exists, check if the passwords match
        query = string.Format("SELECT password FROM user WHERE username = '{0}'", login);

如果用户的数量少于 1,则系统中如何存在用户。

2. 空引用错误:
如果用户不存在,则此方法返回 null,您应该处理 null 并显示不正确的凭据或用户不存在

编辑拉胡尔在问题的评论中似乎是正确的。首先调试代码。

编辑

根据您使用的评论
if (numofUsers <1)
然后你改成了if (用户数 <= 1)
将其用作if (numofUsers> 0)