I'd like to display data that are stored in a SQL server database in a data grid using C#. I tried to follow this examples on msdn, but have encountered a type conversion error. I am using Visual studio 2013.
I am connected to a SQL server, and created an ado.net data model named myEntity. The model contains several tables, one of them, Theater, is what I try to display on the screen.
Here is what I have: On the MainWindow.xaml file I have
<Page x:Class="XYZ.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="theater List" Height="350" Width="525"
Loaded="Data_Loaded">
<Grid>
<DataGrid Name="dataGrid1"></DataGrid>
</Grid>
</Page>
On the MainWindow.xaml.cs file I have:
using System.Data.Entity.Core.Objects;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
namespace XYZ
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public sealed partial class MainPage : Page
{
myEntities dataEntites = new myEntities();
public MainPage()
{
InitializeComponent();
}
private void Data_Loaded(object sender, RoutedEventArgs e)
{
ObjectQuery<Theater> theaters = dataEntites.Theaters;
var query = from theater in theaters
where theater.type == "Big"
orderby theater.id
select new
{
theater.State,
theater.City,
theater.Type,
theater.Id,
theater.Name,
theater.Capacity
...
};
dataGrid1.ItemsSource = query.ToList();
}
}
}
I encountered an error message on the line
ObjectQuery<Theater> theaters = dataEntites.Theaters;
Which states:
Cannot implicitly convert type
'System.Data.Entity.DbSet<XYZ.Theater>'
to'System.Data.Entity.Core.Objects.ObjectQuery<XYZ.Theater>'
How could I fix this? Thanks.