无法访问自动实现的属性

时间:2016-06-30 18:00:27

标签: c# asp.net

Coffee.cs中的代码就像这样

namespace WebTutorial.App_Code
{
public class Coffee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public double Price { get; set; }
    public string Roast { get; set; }
    public string Country { get; set; }
    public string Image { get; set; }
    public string Review { get; set; }

    public Coffee(int id, string name, string type, double price, string      roast, string country, string image, string review)
    {
        Id = id;
        Name = name;
        Type = type;
        Price = price;
        Roast = roast;
        Country = country;
        Image = image;
        Review = review;
    }
}
}

,网页代码就像这样

namespace WebTutorial
{
public partial class Coffee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FillPage();
    }
    private void FillPage()
    {
        ArrayList coffeeList =   ConnectionClass.GetCoffeeByType(DropDownList1.SelectedValue);
        StringBuilder sb = new StringBuilder();

        foreach (Coffee coffee in coffeeList)
        {
            sb.Append(
                string.Format(
                            @"<table class='coffeeTable'>
            <tr>
            <th rowspan='6' width='150px'><img runat='Server' src='{6)'/> </th>
            <th width='50px'>Name: </td>
            <td>{0}</td>
            </tr>

            <tr>
            <th>Type: </th>
            <td>{1}</td>
            </tr>

            <tr>
            <th>Price: </th>
            <td>{2}$</td>
            </tr>

            <tr>
            <th>Roast: </th>
            <td>{3}</td>
            </tr>

            <tr>
            <th>Origin: </th>
            <td>{4}</td>
            </tr>

            <tr>
            <td colspan='2'>{5}</td>
            </tr>
            </table>", coffee.Name, coffee.Type, coffee.Price, coffee.Roast, coffee.Country, coffee.Review, coffee.Image));
        }
        lblOutput.Text = sb.ToString();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillPage();
    }


}
}

现在出现错误,如

  

'WebTutorial.Coffee'不包含'Type'和no的定义   扩展方法'Type'接受类型的第一个参数   可以找到'WebTutorial.Coffee'(你是否错过了使用指令   或汇编参考?)

所有定义的7个属性都会出现错误。

我还在App_code文件中定义了另一个类COnnectionClass.cs,就像这样

namespace WebTutorial.App_Code
{
public static class ConnectionClass
{
    private static SqlConnection conn;
    private static SqlCommand command;

    static ConnectionClass()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["CoffeeConnection"].ToString();
        conn = new SqlConnection(connectionString);
        command = new SqlCommand("",conn);
    }

    public static ArrayList GetCoffeeByType(string coffeeType)
    {
        ArrayList list = new ArrayList();
        string query = string.Format("select * from coffee where type LIKE '{0}'", coffeeType);

        try
        {
            conn.Open();
            command.CommandText = query;
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string name = reader.GetString(1);
                string type = reader.GetString(2);
                double price = reader.GetDouble(3);
                string roast = reader.GetString(4);
                string country = reader.GetString(5);
                string image = reader.GetString(6);
                string review = reader.GetString(7);

                Coffee coffee = new Coffee(id, name, type, price, roast, country, image, review);
                list.Add(coffee);
            }
        }
        finally
        {
            conn.Close();
        }
        return list;
    }
}
}

我错在哪里???这是我说过的视频...... https://www.youtube.com/watch?v=84BdoQr4fKg

我跟着他,也复制了他的代码但得到了同样的错误。

1 个答案:

答案 0 :(得分:2)

您有多个班级Coffee一个班级是您的数据实体

namespace WebTutorial.App_Code
  {
   public class Coffee
    {

另一个是WebPage

namespace WebTutorial
{
  public partial class Coffee : System.Web.UI.Page
  {

将第一堂课重命名为CoffeeInfo