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, new PropertyChangedEventArgs(PropertyName));
}
}
#endregion
}
#endregio
}
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).