Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

DataBinding in WPF Application

Viewed:  2250 
Posted On:  24/11/2013 10:33:44 
How we can connect our WPF application with Data Base ? 

In this article I am going to show how we can connect our WPF application with Data Base. Here I am binding a List Box with DB. I am showing Id, Name, Email, City in a List Box.

Below is my XAML code:

<Window x:Class="DataBindingInWPF.Window1"
    Title="Data binding In WPF" Height="423" Width="368" Loaded="Window_Loaded">
        <ListBox Margin="73,10,29,10" ItemsSource="{Binding Path=EMP}" Name="lstEmployee">
                        <TextBlock Text="{Binding Path=ID}" />
                        <TextBlock Text="{Binding Path=Name}" />
                        <TextBlock Text="{Binding Path=Email}" />
                        <TextBlock Text="{Binding Path=City}" />
                        <TextBlock Text="--------------------" />

Below is my Window.xaml.cs code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Data;
using System.Data.SqlClient; 

namespace DataBindingInWPF
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
         public Window1()

        private void Window_Loaded(object sender, RoutedEventArgs e)

        public void BindData()
            using (SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=Test; Uid=sa; pwd=pwd"))
                SqlCommand cmd = new SqlCommand("Select * From Employee", con);
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(ds, "EMP");
                    lstEmployee.DataContext = ds;
                catch (Exception ex)

When you run the application then Output:

WPF_DB App.png 

Image 1.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us