Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » ADO.NET & Database » Using ADO.NET Entity Data Model in WPF

Using ADO.NET Entity Data Model in WPF

This article will show you how to fetch and show data from database using ADO.NET Entity Data Model in WPF.

Author Rank :
Page Views : 9104
Downloads : 197
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
TestADONETEntityDataModel.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


This article will show you how to fetch and show data from database using ADO.NET Entity Data Model in WPF. What is ADO.NET Entity Data Model? When one takes a look at the amount of code that the average application developer must write to address the impedance mismatch across various data representations (for example objects and relational stores) it is clear that there is an opportunity for improvement. Indeed, there are many scenarios where the right framework can empower an application developer to focus on the needs of the application as opposed to the complexities of bridging disparate data representations.

A primary goal of the upcoming version of ADO.NET is to raise the level of abstraction for data programming, thus helping to eliminate the impedance mismatch between data models and between languages that application developers would otherwise have to deal with. Two innovations that make this move possible are Language-Integrated Query and the ADO.NET Entity Framework. The Entity Framework exists as a new part of the ADO.NET family of technologies. ADO.NET will LINQ-enable many data access components: LINQ to SQL, LINQ to DataSet and LINQ to Entities.

Getting Started:

First of all add a new ADO.NET Entity Data Model template.

Figure1.

1.jpg

Choose your model contents.

Figure2.

2.jpg

Select data connection if already made otherwise make a new connection step by step.

Figure3.

3.jpg

Select database object whatever you want use.

Figure4.

4.jpg

Finally it has .edmx extension that will look like this, here you can update name if you want.

Figure5.

5.jpg

Now let's start work on window or page if you want show data in data grid then you have to add refrence of WPF toolkit (in VisualStudio2008 only). Then you have to add namespace on top of window like this:

xmlns:dataGrid=http://schemas.microsoft.com/wpf/2008/toolkit

here is my data grid.

<Grid>

        <dataGrid:DataGrid AutoGenerateColumns="True" x:Name="MyGrid"></dataGrid:DataGrid>

    </Grid>

public partial class Window2 : Window

    {

        NORTHWNDEntities2 cd;

        public Window2()

        {

            InitializeComponent();

        }

 

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            cd = new NORTHWNDEntities2();

            IEnumerable<Customers> customers = (from p in cd.Customers select p).Take(20);

            MyGrid.ItemsSource = customers;

        }

    }

Output will be like this:

Figure6.

6.jpg

You can set your startup page name App.xaml file.

<Application x:Class="TestADONETEntityDataModel.App"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="Window2.xaml">

    <Application.Resources>

        

    </Application.Resources>

</Application>

If you want show you data in other controls like labels

<Grid>

        <Label Height="28" Margin="19,31,139,0" Name="label1" VerticalAlignment="Top">Company Name: </Label>

        <Label Height="28" Margin="125,31,33,0" Name="CompanyNamelabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="19,56,139,0" Name="label2" VerticalAlignment="Top">Contact Name: </Label>

        <Label Height="28" Margin="125,56,33,0" Name="ContactNamelabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="19,7,0,0" Name="CustomerID" VerticalAlignment="Top" HorizontalAlignment="Left" Width="320">Customer ID</Label>

        <Label Height="28" Margin="125,7,33,0" Name="CustomerIDLabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="12,0,0,56" Name="label3" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="120">Total Customers: </Label>

        <Label Height="28" Margin="125,0,33,56" Name="Countlabel" VerticalAlignment="Bottom"></Label>

        <Label Height="28" HorizontalAlignment="Left" Margin="19,75,0,0" Name="label4" VerticalAlignment="Top" Width="120">Contact Title: </Label>

        <Label Height="28" HorizontalAlignment="Left" Margin="19,98,0,0" Name="label5" VerticalAlignment="Top" Width="120">Country: </Label>

        <Label Height="28" HorizontalAlignment="Left" Margin="19,117,0,0" Name="label6" VerticalAlignment="Top" Width="120">Address: </Label>

        <Label Height="28" HorizontalAlignment="Left" Margin="19,142,0,0" Name="label7" VerticalAlignment="Top" Width="120">City: </Label>

        <Label Height="28" HorizontalAlignment="Left" Margin="19,167,0,0" Name="label8" VerticalAlignment="Top" Width="120">Region: </Label>

        <Label Height="28" Margin="125,75,233,0" Name="ContactTitlelabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="125,99,233,0" Name="Countrylabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="125,117,233,0" Name="Addresslabel" VerticalAlignment="Top"></Label>

        <Label Height="28" Margin="125,142,233,0" Name="Citylabel" VerticalAlignment="Top"></Label>

        <Label Margin="125,167,233,167" Name="Regionlabel"></Label>

    </Grid>

 

Class code:

public partial class Window1 : Window

    {

        NORTHWNDEntities2 ce;

        List<Customers> customersList;

        int currentCustomerIndex = 0;

 

        public Window1()

        {

            InitializeComponent();

        }

 

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            ce = new NORTHWNDEntities2();

            customersList = new List<Customers>();

            IEnumerable<Customers> customerQuery = from ar in ce.Customers select ar;

            customersList = customerQuery.ToList();

            PopulateFields();

          

        }

 

        private void PopulateFields()

        {

            Customers currentCustomer = customersList[currentCustomerIndex];

            if (currentCustomer != null)

            {

                CustomerIDLabel.Content = currentCustomer.CustomerID.ToString();

                CompanyNamelabel.Content = currentCustomer.CompanyName;

                ContactNamelabel.Content = currentCustomer.ContactName;

                Countlabel.Content = customersList.Count.ToString();

                ContactTitlelabel.Content = currentCustomer.ContactTitle;

                Addresslabel.Content = currentCustomer.Address;

                Citylabel.Content = currentCustomer.City;

                Regionlabel.Content = currentCustomer.Region;

                Countrylabel.Content = currentCustomer.Country;

            }

            else

            {

                CustomerIDLabel.Content = "N/A";

                CompanyNamelabel.Content = "N/A";

                ContactNamelabel.Content = "N/A";

                Countlabel.Content = "0";

                ContactTitlelabel.Content = "N/A";

                Addresslabel.Content = "N/A";

                Citylabel.Content = "N/A";

                Regionlabel.Content = "N/A";

                Countrylabel.Content = "N/A";

            }

        }     

    }

Output will look like this:

Figure7.

7.jpg

For more information download attached project, if you have any question and confusion drop me a mail or comment in C# corner comment section.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Raj Kumar

Raj Kumar is a Microsoft MVP and Senior Software Engineer with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, SQL Server 2005/2008, Oracle, WPF, WCF, XAML and Silverlight. He has over 6 years of IT experience working most on Microsoft technologies. He holds Master's degree in Computer Science. When he is not writing code, he likes to write articles and play cricket.

Reach him at raj2511984@gmail.com 

 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Mindcracker MVP Summit 2012
Become a Sponsor
 Comments
helpful by danny On November 11, 2009
thanks man, this is quite helpful!

---------------------------
Danny, Los Angeles Locksmith
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.