Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
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:
Total page views :  3737
Total downloads :  101
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
TestADONETEntityDataModel.zip
 
Become a Sponsor


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.


Login to add your contents and source code to this article
 About the author
 
Raj Kumar
Rajkumar is working as a senior software engineer has over 5 years experience working on ASP.NET, VB.NET, C#, AJAX and other latest technologies. He holds Master's degree in Computer Science. currently enjoying working on WPF, WCF, Silverlight, MVC, XAML.I can be reached on at raj2511984 at yahoo.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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
TestADONETEntityDataModel.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
helpful by danny On November 11, 2009
thanks man, this is quite helpful!

---------------------------
Danny, Los Angeles Locksmith
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.