Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#构造函数有两个参数,但声称它没有一个接受两个参数的构造函数_C#_Class_Error Handling_Constructor - Fatal编程技术网

C#构造函数有两个参数,但声称它没有一个接受两个参数的构造函数

C#构造函数有两个参数,但声称它没有一个接受两个参数的构造函数,c#,class,error-handling,constructor,C#,Class,Error Handling,Constructor,这就是我的问题。我有一个名为Login的类,用于登录和创建新的登录帐户 我创建了一个不带参数的登录构造函数 public Login() { _gloID = 0; _Username = null; _Password = null; _Note = null; _Active = false; _Status = null; _gvoID = 0; _DateModified = new DateTime(1901, 1, 1)

这就是我的问题。我有一个名为Login的类,用于登录和创建新的登录帐户

我创建了一个不带参数的登录构造函数

public Login()
{
    _gloID = 0;
    _Username = null;
    _Password = null;
    _Note = null;
    _Active = false;
    _Status = null;
    _gvoID = 0;
    _DateModified = new DateTime(1901, 1, 1);
    _ModifiedBy = 0;
}
我还创建了一个接受两个参数的登录构造函数。 此构造函数获取用户名和密码,然后从数据库收集其余信息

public Login(string username, string password)
{
    // Declarations
    uint gloid = 0, gvoid = 0, modifiedby = 0;
    string note = null, status = null;
    bool active = false;
    DateTime datemodified = new DateTime(1901, 1, 1);
    // Command
    string query = string.Format("SELECT glo_id, glo_note, glo_active, glo_status, gvo_id, date_modified, modified_by FROM gfrc_login" +
                                    " WHERE glo_username = '{0}' AND glo_password = '{1}'", username, password);

    try
    {
        using (conn)
        {
            conn.Open();
            cmd = new OleDbCommand(query, conn);
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                gloid = Convert.ToUInt32(rdr.GetString(0));
                note = rdr.GetString(1).ToString();
                active = Convert.ToBoolean(rdr.GetString(2));
                status = rdr.GetString(3).ToString();
                gvoid = Convert.ToUInt32(rdr.GetString(4));
                datemodified = Convert.ToDateTime(rdr.GetString(5));
                modifiedby = Convert.ToUInt32(rdr.GetString(6));
            }
        }
    }
    finally
    {
        if (rdr != null)
            rdr.Close();
    }
    if (conn != null)
    {
        conn.Close();
    }

    _gloID = gloid;
    _Username = username;
    _Password = password;
    _Note = note;
    _Active = active;
    _Status = status;
    _gvoID = gvoid;
    _DateModified = datemodified;
    _ModifiedBy = modifiedby;
}
请注意,所有数据库连接变量都已在类的开头声明

现在,当我尝试运行以下命令时,我得到一个错误:“Login”不包含接受2个参数的构造函数

protected void Login_Authenticate(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string password = CalculateMD5(txtPassword.Text);
    Login login = new Login(username, password);
}

编辑:仅供参考,我在代码的其余部分有防止SQL注入的措施。

您可能指的是两个不同的
登录
类。尝试指定全名(使用名称空间)并查看结果。

您从中调用的类也被称为
Login
,我可以从生成的事件处理程序中看出这一点

必须使用该类型的完整命名空间,或将该类重命名为其他名称

例如:

protected void Login_Authenticate(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    string password = CalculateMD5(txtPassword.Text);
    My.Namespace.Login login = new My.Namespace.Login(username, password);
}

您是否意外使用了?您好,SQL注入!(由于表名和
WHERE
关键字之间没有空格,所以SQL格式不正确)。所有恼人的问题最终都会有最简单的解决方案。谢谢。@CyanideGiraffe,这就是为什么它们很讨厌;-)@CyanideGiraffe,考虑把它标记为答案,如果它符合目的。这将使其他人不去猜测其他可能性。