0

我正在使用 Microsoft Visual Web Developer 和 C# based-ASP.Net 开发一个网站作为学校项目。

我已经实现了一个简单的类来表示用户,因为它是在数据库中形成的。该类具有代表 db 中的列的成员,以及一些用于从 DataTable 结果创建实例的静态方法。使用以下代码(MyDbase 是我们获得的一些帮助类):

//User.cs, placed in A
using System;

public class User
{
    private int id;
    private String email;
    private String firstName;
    private String lastName;
    private String username;
    private String passwd;
    private String birthDate;
    private bool isMale;
    private String phone;
    private String avatarUrl;
    private bool isAdmin;

    ... //Accessors and mutators

    public User( int _id, String _email, String _firstName, String _lastName, String _username, String _passwd,
                String _birthDate, bool _isMale, String _phone, String _avatarUrl, bool _isAdmin )
    {
        this.id = _id;
        this.email = _email;
        this.firstName = _firstName;
        this.lastName = _lastName;
        this.username = _username;
        this.passwd = _passwd;
        this.birthDate = _birthDate;
        this.isMale = _isMale;
        this.phone = _phone;
        this.avatarUrl = _avatarUrl;
        this.isAdmin = _isAdmin;
    }

    public static User getUserForSqlResult(System.Data.DataTable result)
    {
        User newUser = null;
        if (result.Rows.Count == 1)
        {
            newUser = new User((int)result.Rows[0]["id"], (String)result.Rows[0]["email"], 
                                (String)result.Rows[0]["firstName"], (String)result.Rows[0]["lastName"], (String)result.Rows[0]["username"],
                                (String)result.Rows[0]["passwd"], (String)result.Rows[0]["birthDate"], (bool)result.Rows[0]["isMale"],
                                (String)result.Rows[0]["phone"], (String)result.Rows[0]["avatarUrl"], (bool)result.Rows[0]["isAdmin"]);
        }

        return newUser;
    }

    public static User getUserById(int id)
    {
        System.Data.DataTable dt = MyDbase.SelectFromTable("select * from tUsers where id=" + id, "db.mdb");
        return getUserForSqlResult(dt);
    }

    public void saveUserChanges()
    {
        MyDbase.ChangeTable("UPDATE tUsers SET email='" + this.email + "', firstName='" + this.firstName + "', lastName='" + this.lastName +
                            "', passwd='" + this.passwd + "', birthDate='" + this.birthDate + "', phone='" + this.phone + "', avatarUrl='" +
                            this.avatarUrl + "' WHERE id=" + this.id , "db.mdb");
    }
}

我尝试运行以下示例代码只是为了确保选择和更新功能正常工作(Default.aspx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        User user = User.getUserById(3);
        user.setEmail("different@email.com");
        user.saveUserChanges();
    }
}

但是,我收到以下错误:

错误 1“System.Security.Principal.IPrincipal”不包含“getUserById”的定义,并且找不到接受“System.Security.Principal.IPrincipal”类型的第一个参数的扩展方法“getUserById”(您是否缺少使用指令还是程序集引用?)

任何想法为什么会发生这种情况?天呐!

4

3 回答 3

4

问题是Page有一个名为User. 当涉及到评估时User.getUserById(),它是在推断而不是你的User班级。请改用完整类型的命名空间:

protected void Page_Load(object sender, EventArgs e)
{

    My.Namespace.User user = My.Namespace.User.getUserById(3);
    user.setEmail("different@email.com");
    user.saveUserChanges();
}

如果您的类没有命名空间,请添加一个。但是,大多数项目都支持默认命名空间。您可以通过查看项目属性来了解这是什么。

于 2013-01-16T18:56:19.720 回答
0

您正在将您的 User 类与页面的User属性混合在一起。

于 2013-01-16T18:57:31.527 回答
0

向您的 User 类添加命名空间以消除 Page.User 属性和您的 User 类之间的歧义

using System;

namespace MyUser
{
    public class User
    {
       ....
    }
}

然后在 Page_Load

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MyUser.User user = MyUser.User.getUserById(3);
        user.setEmail("different@email.com");
        user.saveUserChanges();
    }
}
于 2013-01-16T19:01:07.613 回答