0

I'm developing a website as a school project using Microsoft Visual Web Developer and C# based-ASP.Net.

I have implemented a simple class to represent a User as it is formed in a db. The class Has members that represent the columns in the db, and a couple of static methods to create an instance from a DataTable result. The following code is used (MyDbase is some help class we were given):

//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");
    }
}

I tried to run the following sample code just to make sure the select&update functions are working (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("[email protected]");
        user.saveUserChanges();
    }
}

However, I get the following error:

Error 1 'System.Security.Principal.IPrincipal' does not contain a definition for 'getUserById' and no extension method 'getUserById' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?)

Any ideas why this might happen? Tnx!

3 Answers 3

4

The problem is Page has a property called User. When it comes to evalute User.getUserById() it is inferring that instead of your User class. Use the full type namespace instead:

protected void Page_Load(object sender, EventArgs e)
{

    My.Namespace.User user = My.Namespace.User.getUserById(3);
    user.setEmail("[email protected]");
    user.saveUserChanges();
}

If your class doesn't have a namespace, add one. However, most projects support a default namespace. You can find out what this is by looking at the project properties.

Sign up to request clarification or add additional context in comments.

Comments

0

Add a namespace to your User class to disambiguate between Page.User property and your User class

using System;

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

then in the 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("[email protected]");
        user.saveUserChanges();
    }
}

Comments

0

You are mixing up your User class with the User property of the page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.