【问题标题】:An object reference is required for the non-static field, how do i solve this?非静态字段需要对象引用,我该如何解决?
【发布时间】:2015-10-22 11:36:50
【问题描述】:

我创建了一个 wpf 应用程序,我只是使用 Ado.Net 实体数据模型从我现有的 SQL 服务器数据库中生成了所有内容。

我正在尝试填充我的数据网格,但它一直说这个错误:

非静态字段、方法或对象需要对象引用 属性'PhishFinderDBentitites.PhishingUrls'

这是我在 MainWindow.xaml.cs 中填充数据网格代码的方法:

private void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        {
            ObjectQuery<DataAccess.PhishingUrl> PhishingUrls = PhishFinderDBEntities.PhishingUrls;

            var query =
            from PhishingUrl in PhishingUrls
            orderby PhishingUrl.score
            select new { PhishingUrl.score, PhishingUrl.Path };
            URLGRID.ItemsSource = query.ToList();
        }
    }
}
}

我应该改变什么?

【问题讨论】:

  • PhishFinderDBEntities 实体 = 新的 PhishFinderDBEntities();
  • @Gusman PhishFinderDBEntities 实体 = 新的 PhishFinderDBEntities(); ObjectQuery PhishingUrls = PhishFinderDBEntities.PhishingUrls;我这样做没有帮助
  • @Gusman 也许我放错地方了?我应该把它放在哪里?
  • 告诉我们你是如何初始化/创建这个对象PhishFinderDBEntities
  • @Waqar Ahmed, public MainWindow() { InitializeComponent(); PhishFinderDBEntities 实体 = 新的 PhishFinderDBEntities(); }

标签: c# sql-server wpf datagrid


【解决方案1】:

您没有创建实体的实例,您需要实例化它并使用其中的集合:

private void DataGrid_Loaded(object sender, RoutedEventArgs e)
{
    {
        var entities = new PhishFinderDBEntities();
        var PhishingUrls = entities.PhishingUrls;

        var query =
        from PhishingUrl in PhishingUrls
        orderby PhishingUrl.score
        select new { PhishingUrl.score, PhishingUrl.Path };
        URLGRID.ItemsSource = query.ToList();
    }
}

【讨论】:

    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多