0% found this document useful (0 votes)
76 views

Xaml Code:: WPF Database

This document contains code for a WPF application that allows users to access and manage employee data stored in an Access database. The XAML code defines the user interface which includes fields to add/edit employees and a datagrid to view records. The C# code handles connecting to the database, executing SQL statements to retrieve, add, update and delete employee records, and populating the datagrid. Buttons allow adding new records, editing existing ones, deleting rows, and clearing form fields.

Uploaded by

Akmal Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Xaml Code:: WPF Database

This document contains code for a WPF application that allows users to access and manage employee data stored in an Access database. The XAML code defines the user interface which includes fields to add/edit employees and a datagrid to view records. The C# code handles connecting to the database, executing SQL statements to retrieve, add, update and delete employee records, and populating the datagrid. Buttons allow adding new records, editing existing ones, deleting rows, and clearing form fields.

Uploaded by

Akmal Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

WPF Database

Xaml Code:
<Window x:Class="WPF_AccessDB.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF CECOS SChool Database Example" Height="700" Width="900"
Background="Bisque" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" >
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20"/>
<Setter Property="Width" Value="160"/>
<Setter Property="Margin" Value="10"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="20"/>
<Setter Property="Width" Value="250"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<DockPanel Name="dockMain" VerticalAlignment="top" HorizontalAlignment="Center"
LastChildFill="False">
<StackPanel>
<Label Content="CECOS School" HorizontalAlignment="Center" FontSize="36"
FontWeight="Bold"></Label>
<WrapPanel>
<TextBlock Text="Employee Id : "/>
<TextBox Name="txtEmpId" />
</WrapPanel>
<WrapPanel>
<TextBlock Text="Employee Name : "/>
<TextBox Name="txtEmpName" />
</WrapPanel>
<WrapPanel>
<TextBlock Text="Gender : "/>
<ComboBox FontSize="20" Name="ddlGender" Width="250" Margin="10">
<ComboBoxItem Content="Select Gender"
Selector.IsSelected="True" />
<ComboBoxItem Content="Male" />
<ComboBoxItem Content="Female" />
</ComboBox>
</WrapPanel>
<WrapPanel>
<TextBlock Text="Contact : "/>
<TextBox Name="txtContact" />
</WrapPanel>
<WrapPanel>
<TextBlock Text="Address : "/>
<TextBox Name="txtAddress" TextWrapping="Wrap"
AcceptsReturn="True" Height="75" />
</WrapPanel>
<WrapPanel Margin="0" HorizontalAlignment="Center" Height="59">
<Button Name="btnAdd" Content="Add" FontSize="25" Width="120"
Margin="5" Click="btnAdd_Click" />
<Button Name="btnEdit" Content="Edit" FontSize="25"
Width="120" Margin="5" Click="btnEdit_Click" />
<Button Name="btnDelete" Content="Delete" FontSize="25"
Width="120" Margin="5" Click="btnDelete_Click" />
<Button Name="btnCancel" Content="Cancel" FontSize="25"
Width="120" Margin="5" Click="btnCancel_Click" />
<Button Name="btnExit" Content="Exit" FontSize="25"
Width="120" Margin="5" Background="#400000" Foreground="Bisque" Click="btnExit_Click"
/>
</WrapPanel>
<Label Content="No records found." Name="lblCount"
HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"
Foreground="#FFE10000"></Label>
<WrapPanel Margin="20" HorizontalAlignment="Center">
<DataGrid AutoGenerateColumns="True" Name="gvData"
SelectionMode="Single" FontSize="15" Padding="5" Background="Black" />
</WrapPanel>
</StackPanel>
</DockPanel>
</Window>

C# 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.OleDb;
using System.Data;

namespace WPF_AccessDB
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
OleDbConnection con;
DataTable dt;

public MainWindow()
{
InitializeComponent();
//Connect your access database
con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" +
AppDomain.CurrentDomain.BaseDirectory + "\\EmpDB.mdb";
BindGrid();
}

//Display records in grid


private void BindGrid()
{
OleDbCommand cmd = new OleDbCommand();
if (con.State != ConnectionState.Open)
con.Open();
cmd.Connection = con;
cmd.CommandText = "select * from tbEmp";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
gvData.ItemsSource = dt.AsDataView();

if (dt.Rows.Count > 0)
{
lblCount.Visibility = System.Windows.Visibility.Hidden;
gvData.Visibility = System.Windows.Visibility.Visible;
}
else
{
lblCount.Visibility = System.Windows.Visibility.Visible;
gvData.Visibility = System.Windows.Visibility.Hidden;
}

//Add records in grid


private void btnAdd_Click(object sender, RoutedEventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
if (con.State != ConnectionState.Open)
con.Open();
cmd.Connection = con;

if (txtEmpId.Text != "")
{
if (txtEmpId.IsEnabled == true)
{
if (ddlGender.Text != "Select Gender")
{
cmd.CommandText = "insert into
tbEmp(EmpId,EmpName,Gender,Contact,Address) Values(" + txtEmpId.Text + ",'" +
txtEmpName.Text + "','" + ddlGender.Text + "'," + txtContact.Text + ",'" +
txtAddress.Text + "')";
cmd.ExecuteNonQuery();
BindGrid();
MessageBox.Show("Employee Added Successfully...");
ClearAll();

}
else
{
MessageBox.Show("Please Select Gender Option....");
}
}
else
{
cmd.CommandText = "update tbEmp set EmpName='" + txtEmpName.Text +
"',Gender='" + ddlGender.Text + "',Contact=" + txtContact.Text + ",Address='" +
txtAddress.Text + "' where EmpId=" + txtEmpId.Text;
cmd.ExecuteNonQuery();
BindGrid();
MessageBox.Show("Employee Details Updated Succesffully...");
ClearAll();
}
}
else
{
MessageBox.Show("Please Add Employee Id.......");
}
}

//Clear all records from controls


private void btnCancel_Click(object sender, RoutedEventArgs e)
{
ClearAll();
}

private void ClearAll()


{
txtEmpId.Text = "";
txtEmpName.Text = "";
ddlGender.SelectedIndex = 0;
txtContact.Text = "";
txtAddress.Text = "";
btnAdd.Content = "Add";
txtEmpId.IsEnabled = true;
}

//Edit records
private void btnEdit_Click(object sender, RoutedEventArgs e)
{
if (gvData.SelectedItems.Count > 0)
{
DataRowView row = (DataRowView)gvData.SelectedItems[0];
txtEmpId.Text = row["EmpId"].ToString();
txtEmpName.Text = row["EmpName"].ToString();
ddlGender.Text = row["Gender"].ToString();
txtContact.Text = row["Contact"].ToString();
txtAddress.Text = row["Address"].ToString();
txtEmpId.IsEnabled = false;
btnAdd.Content = "Update";
}
else
{
MessageBox.Show("Please Select Any Employee From List...");
}
}
//Delete records from grid
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (gvData.SelectedItems.Count > 0)
{
DataRowView row = (DataRowView)gvData.SelectedItems[0];

OleDbCommand cmd = new OleDbCommand();


if (con.State != ConnectionState.Open)
con.Open();
cmd.Connection = con;
cmd.CommandText = "delete from tbEmp where EmpId=" +
row["EmpId"].ToString();
cmd.ExecuteNonQuery();
BindGrid();
MessageBox.Show("Employee Deleted Successfully...");
ClearAll();
}
else
{
MessageBox.Show("Please Select Any Employee From List...");
}
}

//Exit
private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}

ScreenShort:

You might also like