I am using Visual Studio 2012 C Sharp (C#) and SQL Server Express. I am trying to go through the MSDN Walkthrough: Display Data from a SQL Server Database in a DataGrid Control. I have added the recommended AdventureWorksLT2008 sample database. Here are the instructions I followed:
- Create a new WPF Application project in Visual Basic or C#, and name it DataGridSQLExample.
- In Solution Explorer, right-click your project, point to Add, and then select New Item. ( The Add New Item dialog box appears. )
- In the Installed Templates pane, select Data and in the list of templates, select ADO.NET Entity Data Model.
- Name the file
AdventureWorksModel.edmx
and then click Add. (The Entity Data Model Wizard appears.) - In the Choose Model Contents screen, select Generate from database and then click Next.
- In the Choose Your Data Connection screen, provide the connection to your AdventureWorksLT2008 database.
- Make sure that the name is AdventureWorksLT2008Entities and that the Save entity connection settings in
App.Config
as check box is selected, and then click Next. - In the Choose Your Database Objects screen, expand the Tables node, and select the Product and ProductCategory tables.
- Click Finish.
To retrieve and present the data:
- Open the
MainWindow.xaml
file. - Set the Width property on the Window to 450.
- In the XAML editor, add the following DataGrid tag between the
<Grid>
and</Grid>
tags to add a DataGrid nameddataGrid1
. - Select the Window.
- Using the Properties window or XAML editor, create an event handler for the Window named
Window_Loaded
for the Loaded event. - Open the code-behind file (
MainWindow.xaml.vb
orMainWindow.xaml.cs
) for the Window. Add the following code to retrieve only specific values from the joined tables and set the
ItemsSource
property of the DataGrid to the results of the query:using System; using System.Collections.Generic; using System.Data.Objects; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace DataGridSQLExample { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { AdventureWorksLT2008Entities dataEntities = new AdventureWorksLT2008Entities(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { ObjectQuery<Product> products = dataEntities.Products; var query = from product in products where product.Color == "Red" orderby product.ListPrice select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice }; dataGrid1.ItemsSource = query.ToList(); } } }
- Run the example. You should see a DataGrid that displays data.
When I run I get this error:
Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Objects.ObjectQuery' on this line: ObjectQuery products = dataEntities.Products;
Nothing I have tried works - any suggestions?