我的代码不适用于视觉工作室

时间:2012-08-15 10:29:01

标签: c# visual-studio-2010 methods

我有一个错误,说我没有一个名为'GetUser'的方法,即使它在那里..错误说: “System.Security.Principal.IPrincipal”不包含'GetUser'的定义,并且没有扩展方法'GetUser'接受类型'System.Security.Principal.IPrincipal'的第一个参数可以找到“

这是我在User class

中的代码
public static List<User> GetUser()
    {
        SqlConnection conn = null;
        List<User> result = new List<User>();
        try
        {
            conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["2ndEardbConnectionString"].ConnectionString;
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            comm.CommandText = "select * from User";
            SqlDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {
                User u = new User();
                u.Name = (string)dr["Name"];
                u.UserName = (string)dr["UserName"];
                u.Password = (string)dr["Password"];
                u.DOB = (DateTime)dr["DOB"];
                u.Gender = (string)dr["Gender"];
                u.Email = (string)dr["Email"];
                u.ContactNumber = (int)dr["ContactNumber"];
                u.ProfilePic = (byte)dr["ProfilePic"];
                u.Image = (byte)dr["Image"];


                result.Add(u);
            }
            dr.Close();
        }
        catch (SqlException e)
        {
            throw e;
        }
        finally
        {
            conn.Close();
        }
        return result;
    }

并且在表单中,这是我放的代码

protected void Button1_Click(object sender, EventArgs e)
    {
        List<User> results = User.GetUser();
        foreach (User u in results)
        {
            if (TextBox8.Text.ToString() == u.UserName.ToString())
            {
                Label13.Visible = true;
                break;
            }
            else
            {
                Label12.Visible = true;
            }
        }
    }

但是,User.GetUser()发生错误:

声明没有方法。

我如何在视觉工作室中解决这个问题C#  请帮忙 谢谢

1 个答案:

答案 0 :(得分:4)

您没有调用GetUser方法,而是调用System.Security.Principal.IPrincipal方法。使用完整命名空间。

List<User> results = MyNamespace.User.GetUser();