You are on page 1of 26

I think this Tutorial Useful for beginners.

Next Using C# [INotifyPropertyChanged]


Continue in Next Chapter
Download
Download source code for WPF Data Binding Tutorial 1

Introduction
Windows Presentation Foundation (WPF) is ships part of .NET frame work and Develop
our GUI application 2D,3D,graphics animation and media content using in this WPF.
In advantage of WPF to less your coding in application.and Easier animation and special
effects,Inherent Scalability Inherent scalability

The developer has to manually write the Code update each UI Elements That Display
value stored in the object member .In WPF you can less your code greatly by binding
the object directly with the UI using XAML syntax.A developer typically creates a class
to represent an entity (table) in the database,where CRUD operations performed by
Calling methods on the objects.
I will Converting 3 different approaches for displaying data in the object onto the UI
using WPF

Basic of WPF Binding


Using c#[INotifyPropertyChanged]
Using c# and XAML[INotifyPropertyChanged and XAML]

Create Table:

I am going use the same "StudentInformation" table.The Columns for the table are

RollNo(int,not null)
Fname(varchar(10),not null)
Lname(varchar(10),not null)
Clgname(varchar(50),not null)
mark(int,not null)
Exam(varchar(15),not null)

Basics of WPF Binding:


First creates the Students class for the table as follows: That Class contain
Following Codes

Properties:

Properties

Insert Record
Update Record
Load Record

Using a property , a programmer can get access to data members as through they are
public fields, The set and Get methods of property are Contained inside the property
declaration.
Syntax:
private string _myName;
// Declare a myName property of type string
public string myName
{
get
{ return _myName;}

set {

myName = _value; }

}
The get method used the keyword return to return the field's value to caller.The set
method use the keyword value to receive the value being passed in from the user.
A property can omit either a get clause or the set clause,A property that has get is
called a Read-only set is called write-only property.
Create the "Students" class for the table as follows,The Set and Get methods
of property are continued inside the property declaration

class Students
{
private int _Rollno;
public int Rollno// Declare a Rollno property of type int
{
get { return _Rollno;}
set { _Rollno = value;}
}
private string _FirstName;
public string FirstName// Declare a FirstName property of type string
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string_Lastname;
public string Lastname// Declare a LastName property of type string
{
get { return _Lastname; }
set { _Lastname = value;}
}
private string _Collegename;
public string Collegename// Declare a CollegeName property of type string
{
get { return _Collegename; }
set { _Collegename = value;}
}
private int _Marksobtaine;
public int Marksobtaine// Declare a Marksobtaine property of type int
{
get { return _Marksobtaine; }
set { _Marksobtaine = value;}
}
private string _Exam;
public string Exam// Declare a Exam property of type string

{
get { return _Exam; }
set { _Exam = value;}
}
}
Insert Record

Here insert record we already create table StudenteInformation table. using


SqlConnection craete connection and open a connection "objCon.Open();",
use insert query to insert records using "SqlCommand".finally execute query and
close connection.
public void Insetrt()
{
SqlConnection objCon

new

//create

SqlConnection("[.]");

connection
objCon.Open(); //open Connection
objcmd = new SqlCommand("insert into StudentInformation
values('"+_Rollno+"','"+_FirstName+"','"+_Lastname+"','"+_Marksobtaine
objcmd.ExecuteNonQuery();
}
Load Record:

Here view record based on Rollno using "SqlDataReader",The ExecuteReader method


of the SqlCommand objectcmd ,returns a SqlDataReader instance.Creating a
SqlDataReader with the new operator doesn't do anything for you.
You must call ExecuteReader on a command object,like this
SqlDataReader rdr = cmd.ExecuteReader();

In our Source Coding Read data from records and binding the value from particular property name
public void Load()
{
SqlConnection objCon = new SqlConnection("[.]");
objCon.Open();
objcmd = new SqlCommand("Select * from
StudentInformation
where Rollno=@Rollno", objCon);
//select

command
objcmd.Parameters.Add(new SqlParameter("@Rollno", _Rollno));
SqlDataReader objRDR = objcmd.ExecuteReader(); //read data

from records(table)
if (objRDR.Read())

//Bind records in

property

name
{
_FirstName = objRDR["Fname"].ToString();
_Lastname = objRDR["Lname"].ToString();
_Colleagename = objRDR["Clgname"].ToString();
_Marksobtaine = Convert.ToInt32(objRDR["Mark"]);
_Exam = objRDR["Exam"].ToString();
}
else
{
MessageBox.Show("Record Not Found" );

}
}
Update Records:

Here we update record based


particular property name example

on

update

query,

we

first

assign

field

to

Fname=@Fname,Lname=@Lname,Mark=@Mark,Clgname=@Clgname

public void update()


{
SqlConnection objCon = new SqlConnection("[..]");
objCon.Open();
objcmd = new SqlCommand("update StudentInformation set

//assign the values and add parameters


objcmd.Parameters.AddWithValue("@Fname",_FirstName);
objcmd.Parameters.AddWithValue("@Lname", _Lastname);
objcmd.Parameters.AddWithValue("@Clgname", _Colleagename);
objcmd.Parameters.AddWithValue("@Mark", _Marksobtaine);
objcmd.ExecuteNonQuery();
}
UI Side:

I figured that since it is XAML that we are demonstrating, might as well develop the UI
in Microsoft Cider, the Visual Studio 2010 add-in visual designer for XAML. So, to use
the above class the coding for XAML.
<Window x:Class="WPF_Tutorial.Dataentry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Dataentry" Height="443" Width="371"
Background="WhiteSmoke" WindowStartupLocation="CenterScreen">
<Grid Height="285" Width="286">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="99*"></ColumnDefinition>
<ColumnDefinition Width="187*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="54,20,0,0">Rollno::</TextBlock>
<TextBlock Grid.Row="1" Margin="39,12,0,12">FirstName::</TextBlock>
<TextBlock Grid.Row="2" Margin="34,9,0,14">LastName::</TextBlock>
<TextBlock
Grid.Row="3"
Margin="22,10,185,12"
Grid.ColumnSpan="2">CollegeName::</TextBlock>
<TextBlock
Grid.Row="4"
Margin="62,10,185,13"
Grid.ColumnSpan="2">Mark::</TextBlock>

<TextBlock Grid.Row="5" Margin="33,10,3,17">ExamResult::</TextBlock>


<TextBox
Grid.Row="0"
Grid.Column="1"
Name="txtRollNo"
Margin="0,7,40,10"></TextBox>
<TextBox
Grid.Row="1"
Grid.Column="1"
Name="txtFirstName"
Margin="0,7,40,10"></TextBox>
<TextBox
Grid.Row="2"
Grid.Column="1"
Name="txtLastName"
Margin="0,7,40,10"></TextBox>
<TextBox
Grid.Row="3"
Grid.Column="1"
Name="txtClgname"
Margin="0,7,40,10"></TextBox>
<TextBox
Grid.Row="4"
Grid.Column="1"
Name="txtMarks"
Margin="0,7,40,10"></TextBox>
<TextBox
Grid.Row="5"
Grid.Column="1"
Name="txtexamresult"
Margin="0,7,40,10"></TextBox>
<Button Grid.Row="6" Name="btnAdd" Content="Insert" Margin="12,12,14,0"
Click="btnAdd_Click"></Button>
<Button
Grid.Row="6"
Grid.Column="1"
Name="btnLoad"
Margin="13,12,102,0" Click="btnLoad_Click"></Button>
<Button Grid.Row="6" Grid.Column="1" Name="btnUpdate"
Margin="91,12,12,0" Click="btnUpdate_Click"></Button>

Content="Load"

Content="Update"

</Grid>
</Window>

Use above coding design our UI in .XAML page


The app lets the user enter the Roll Number for a student, and displays the Student's
stats when the "Load" button is clicked. The user can then make any changes in values
if required (except for the Roll Number), and click "Update" to make the changes in the
database. Nothing fancy here. The code-behind file is pretty straight-forward as well. I
have placed a "Check" button on the form to let the user view and compare the values
in both, the Student object and on the UI.
In button Click Event no need to write coding, just call object from Student Class and
binding the values
create object for Student Class
Students objStudent = new Students();

objStudent is object of student class now we call methods for inside load, insert
and update Button Click Event

like this
objStudent.Insert();
objStudent.Load();
objStudent.update();

After call objects we binding the Textbox value in particular Property name
In our Source Code

// insert Record
private void btnAdd_Click(object sender,RoutedEventArgs e)
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.FirstName = txtFirstName.Text;
objStudent.Lastname = txtLastName.Text;
objStudent.Colleagename = txtClgname.Text;
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
objStudent.Exam = txtexamresult.Text;
objStudent.Insert();
//call object for insert record
MessageBox.Show("Record inserted", "Insert");
}
//load record
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
objStudent.Rollno =Convert.ToInt32(txtRollNo.Text);
objStudent.Load(); //call object for Load record
txtFirstName.Text =objStudent.FirstName;
txtLastName.Text =objStudent.Lastname;
txtClgname.Text =objStudent.Colleagename;
txtMarks.Text =Convert.ToString(objStudent.Marksobtaine);
txtexamresult.Text =objStudent.Exam;
}
//update record
private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.FirstName = txtFirstName.Text;
objStudent.Lastname = txtLastName.Text;
objStudent.Colleagename = txtClgname.Text;
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
objStudent.Exam = txtexamresult.Text;
objStudent.update();
//call object for update record
MessageBox.Show("Update Record", "Update");
}

This articles would be helpful for Beginners to learn WPF data binding.
Download
Download source code for WPF Data Binding Tutorial 2

WPF Data Binding Tutorial 2

Introduction
We already saw basics of WPF Binding in Chapter 1 (refer WPF Data Binding Tutorial 1)

Now we would see the next topic i.e. Using C#[INotifyPropertyChanged].In this
chapter, I would cover usage of INotifyPropertyChanged and how to bind data in WPF.
What is the purpose, we need to use INotifyPropertyChanged?
Event is
fired whenever a property Changed. In the following example we see how the UI can
know that this event is fired.

We achieve same resulst in implemented the INotifyPropertyChanged interface


in the Student class to generate an event every time the value in the object's property
changes.I have highlighted some codes in below example that"s need to be added to
implement INotifyPropertyChanged.

create class the class name is Student


Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows;

using System.ComponentModel;
using System.Data;

namespace WPF_Tutorial
{
public

class Students:INotifyPropertyChanged

{
#region Studentdetails

private int _Rollno;


public int Rollno
{
get{ return _Rollno; }
set{ _Rollno = value;

OnPropertyChanged("Rollno");
}
}
private string _FirstName;
public string FirstName
{
get{ return _FirstName; }
set{ _FirstName = value;

OnPropertyChanged("FirsName");
}
}
private string _Lastname;
public string Lastname
{
get{ return _Lastname; }
set{ _Lastname = value;

OnPropertyChanged("Lastname");
}
}
private string _Colleagename;
public string Colleagename
{

get{ return _Colleagename; }


set{ _Colleagename = value;

OnPropertyChanged("Colleagename");
}
}
private int _Marksobtaine;
public int Marksobtaine
{
get{ return _Marksobtaine; }
set{ _Marksobtaine = value;

OnPropertyChanged("Marksobtaine");
}
}
private string _Exam;
public string Exam
{
get{ return _Exam; }
set{ _Exam = value;

OnPropertyChanged("Exam");
}
}
#endregion
public Students()
{
}

public SqlCommand objcmd;

#region insertDetail
public void Insetrt()
{
SqlConnectionobjCon = new SqlConnection("");//connection name
try
{
objCon.Open();
objcmd = new SqlCommand("insert into StudentInformation
values(@Rollno,@Fname,@Lname,@Mark,@clgname,@Exam) ", objCon);
objcmd.Parameters.AddWithValue("@Rollno", Rollno);
objcmd.Parameters.AddWithValue("@Fname", FirstName);
objcmd.Parameters.AddWithValue("@Lname", Lastname);
objcmd.Parameters.AddWithValue("@clgname", Colleagename);
objcmd.Parameters.AddWithValue("@Mark", Marksobtaine);
objcmd.Parameters.AddWithValue("@Exam", Exam);
//On property

changed

OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
objcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
finally
{

objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();

}
}
}
#endregion
#region LoadDetails
public void Load()
{
SqlConnection objCon = new SqlConnection(""); //connection name
objCon.Open();
objcmd = new SqlCommand("Select * from StudentInformation where
Rollno=@Rollno", objCon);//select command
objcmd.Parameters.Add(new SqlParameter("@Rollno", _Rollno));
try
{
SqlDataReader objRDR = objcmd.ExecuteReader();
if(objRDR.Read())
{
_FirstName = objRDR["Fname"].ToString();
_Lastname = objRDR["Lname"].ToString();
_Colleagename =objRDR["Clgname"].ToString();
_Marksobtaine = Convert.ToInt32(objRDR["Mark"]);
_Exam = objRDR["Exam"].ToString();

//property changed

OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
}
else
{
MessageBox.Show("RecordNot Found");
}
}
catch(Exception ex)
{
MessageBox.Show("" + ex);
}
finally
{
objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();
}

}
}

#endregion
#region UpdateDetails
public void update()
{

SqlConnection objCon = new SqlConnection(""); //connection name


objCon.Open();
objcmd = new SqlCommand("update
StudentInformation set
Fname=@Fname,Lname=@Lname,Mark=@Mark,Clgname=@Clgname,Exam=@Exam
where
Rollno=@Rollno ", objCon);
objcmd.Parameters.Add(new SqlParameter("@Rollno", _Rollno));
try
{
objcmd.Parameters.AddWithValue("@Fname", FirstName);
objcmd.Parameters.AddWithValue("@Lname", Lastname);
objcmd.Parameters.AddWithValue("@Clgname", Colleagename);
objcmd.Parameters.AddWithValue("@Mark", Marksobtaine);
objcmd.Parameters.AddWithValue("@Exam", Exam);
//propertychanged

OnPropertyChanged("Rollno");
OnPropertyChanged("FirstName");
OnPropertyChanged("Lastname");
OnPropertyChanged("Colleagename");
OnPropertyChanged("Marksobtaine");
OnPropertyChanged("Exam");
objcmd.ExecuteNonQuery();
}
catch (Exception ex)

{
MessageBox.Show("" + ex);
}
finally
{
objcmd.Dispose();
if(objCon.State == ConnectionState.Open)
{
objCon.Close();
objCon.Dispose();
}
}
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;


protected void OnPropertyChanged(string PropertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this,
PropertyChangedEventArgs(PropertyName));
}
}
#endregion
}
#endregio
}

new

The INotifyPropertyChanged interface implementation allows the class to raise the


PropertyChanged event everytime a property value changes. The above class can now
be instanitated in the code-behind file of another XAML file (WithoutXAML.xaml) which
is similar to that of code listing in below(UI Side).
UI Side:

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.ComponentModel;
namespace WPF_Tutorial
{
public partial class Dataentry : Window
{
Students objStudent = new Students();
public Dataentry()
{
InitializeComponent();
}
//insert record

private void btnAdd_Click(object sender, RoutedEventArgs e)


{
try
{
objStudent.Insetrt();

MessageBox.Show("Record inserted", "Insert");


//clearField

txtRollNo.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtClgname.Text = "";
txtexamresult.Text = "";
txtMarks.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//load record
private void btnLoad_Click(object sender, RoutedEventArgs e)
{
if (txtRollNo.Text != null)
{
try
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.Load();
}
catch(Exception ex)

{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("enter correct code");
}
}

//update record

private void btnUpdate_Click(object sender, RoutedEventArgs e)


{
if (txtRollNo.Text != null)
{
try
{
objStudent.Rollno = Convert.ToInt32(txtRollNo.Text);
objStudent.update();
MessageBox.Show("Update Record", "Update");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{

objStudent.PropertyChanged += objStudent_PropertyChanged;
}

// Property Changed Event Handler [Pathetic Code!]

private
void
objStudent_PropertyChanged(object
sender,
PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "Rollno":
txtRollNo.Text = Convert.ToString(objStudent.Rollno);
break;
case "FirstName":
txtFirstName.Text =objStudent.FirstName;
break;
case "Lastname":
txtLastName.Text = objStudent.Lastname;
break;
case "Colleagename":
txtClgname.Text = objStudent.Colleagename;
break;
case "Marksobtaine":
txtMarks.Text
=
Convert.ToString(objStudent.Marksobtaine);
break;
case "Exam":
txtexamresult.Text = objStudent.Exam;
break;
}
}

private void txtRollNo_TextChanged(object sender, TextChangedEventArgs e)


{
objStudent.Rollno =Convert.ToInt32(txtRollNo.Text);
}

private void txtFirstName_TextChanged(object sender, TextChangedEventArgs e)


{
objStudent.FirstName = txtFirstName.Text;
}
private void txtLastName_TextChanged(object sender, TextChangedEventArgs e)
{
objStudent.Lastname = txtLastName.Text;
}
private void txtClgname_TextChanged(object sender, TextChangedEventArgs e)
{
objStudent.Colleagename =

txtClgname.Text;

}
private void txtMarks_TextChanged(object sender, TextChangedEventArgs e)
{
objStudent.Marksobtaine = Convert.ToInt32(txtMarks.Text);
}
private void txtexamresult_TextChanged(object sender, TextChangedEventArgs
e)
{
objStudent.Exam = txtexamresult.Text;
}
}
}

Note that the objStudent_PropertyChanged event handler updates the UI with


object
values
everytime
a
PropertyChanged
event
occurs
on
the Student object. To update the object with UI values, TextChanged event
handlers
were
added
for
all
the
textboxes

Note also that the Insert "Load" and "Update" button event handlers do not
contain the Object-to-UI and UI-to-Object update code as that is now being
handled
by
the
event
handlers.
However, just because we implemented the INotifyPropertyChanged interface
in our Student class, we were not absolved of the responsibility to write custom event
handlers for the object and UI update logic. The whole point of all the above code is to
show the tedious coding required to keep the object and UI in sync.

Conclusion
In this articles, we learnt using INotifyPropertyChanged
and how property
Changed.Next Chapter, we will see Using C# and XAML [INotifyPropertyChanged and
XAML]
it's useful for beginners
Download
Download source code for WPF Data Binding Tutorial 3

WPF Data Binding Tutorial 3:


Introduction
We
already
saw Basic
of
(refer:http://tempuri.org/tempuri.html)

WPF
Binding in
Chapter1
and C#[INotifyPropertyChanged] in

Chapter 2(refer:http://tempuri.org/tempuri.html ).

Now we will explore Using C# and XAML[INotifyPropertyChanged]. In this article


,the Data Binding Syntax in XAML is used and would show you how easy it is,to bind UI
elements directly to an object that implements INotifyPropertyChanged.
To see WPF Data Binding
"Text" property values.

in

action

,the

Binding

syntax

in

the

textboxes

I have highlighted some codes in below example that"s needed to be added to


implement INotifyPropertyChanged.We
already
know
the
INotifyPropertyChanged
implemented
in Chapter
2(refer:http://tempuri.org/tempuri.html ).Now we see XAML page how to
binding use Binding Property in Text below code i highlighted some codes in
below XAML

<Window x:Class="WPF_Tutorial.Dataentry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Dataentry" Height="443" Width="371"
Background="WhiteSmoke"
Loaded="Window_Loaded">

WindowStartupLocation="CenterScreen"

<Grid Height="285" Width="286" x:Name="myGrid">


<Grid.ColumnDefinitions>
<ColumnDefinition Width="99*"></ColumnDefinition>
<ColumnDefinition Width="187*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Margin="54,20,0,0">Rollno::</TextBlock>
<TextBlock Grid.Row="1" Margin="39,12,0,12">FirstName::</TextBlock>
<TextBlock Grid.Row="2" Margin="34,9,0,14">LastName::</TextBlock>

<TextBlock
Grid.Row="3"
Grid.ColumnSpan="2">CollegeName::</TextBlock>

Margin="22,10,185,12"

<TextBlock
Grid.ColumnSpan="2">Mark::</TextBlock>

Margin="62,10,185,13"

Grid.Row="4"

<TextBlock Grid.Row="5" Margin="33,10,3,17">ExamResult::</TextBlock>


<TextBox Grid.Column="1" Name="txtRollNo"

Margin="0,7,40,10"

Text="{Binding Path=Rollno}" ></TextBox>


<TextBox Grid.Row="1" Grid.Column="1" Name="txtFirstName" Text="{Binding
Path=FirstName}" Margin="0,7,40,10" ></TextBox>

Name="txtLastName"

<TextBox
Margin="0,7,40,10"

Grid.Row="2"

Grid.Column="1"

Text="{Binding Path=Lastname}" ></TextBox>


<TextBox Grid.Row="3" Grid.Column="1" Name="txtClgname"

Margin="0,7,40,10"

Text="{Binding Path=Colleagename}" ></TextBox>


<TextBox Grid.Row="4" Grid.Column="1" Name="txtMarks"

Margin="0,7,40,10"

Text="{Binding Path=Marksobtaine}"></TextBox>

Name="txtexamresult"

<TextBox
Margin="0,7,40,10"

Grid.Row="5"

Grid.Column="1"

Text="{Binding Path=Exam}" ></TextBox>


<Button Grid.Row="6"
Click="btnAdd_Click"></Button>

Name="btnAdd"

Content="Insert"

Margin="12,12,14,0"

<Button
Grid.Row="6"
Grid.Column="1"
Name="btnLoad"
Margin="13,12,102,0" Click="btnLoad_Click"></Button>
<Button Grid.Row="6" Grid.Column="1" Name="btnUpdate"
Margin="91,12,12,0" Click="btnUpdate_Click"></Button>

Content="Load"

Content="Update"

</Grid>
</Window>

The Binding Statement added to the XAML above allows you to discard all the eventhandlers in the code-behind that were need before.The function that we wanted from
our app is now achievable with a lot less code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.ComponentModel;
namespace
WPF_Tutorial
{
public partial class Dataentry : Window
{
Students objStudent = new Students(); //create object for student class
publicDataentry()
{
InitializeComponent();
}
//insert record
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
try
{
objStudent.Insetrt();
MessageBox.Show("Record inserted", "Insert");
//clearField
txtRollNo.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";

txtClgname.Text = "";
txtexamresult.Text = "";
txtMarks.Text = "";
}
catch(Exception ex)

{
MessageBox.Show(ex.Message);
}
}
//load record
private void btnLoad_Click(objectsender, RoutedEventArgs e)
{
if(txtRollNo.Text != null)
{
try
{
objStudent.Load();

catch(Exception ex)
{

MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("enter correct code");

}
}
//updaterecord
private void btnUpdate_Click(objectsender, RoutedEventArgs e)

{
if(txtRollNo.Text != null)
{
try
{

objStudent.update();
MessageBox.Show("Update Record", "Update");

catch(Exception ex)

{
MessageBox.Show(ex.Message);

}
}
}

private void Window_Loaded(objectsender, RoutedEventArgs e)

{
myGrid.DataContext = objStudent;
}

}
}

You might also like