Xaml Code:: WPF Database
Xaml Code:: 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();
}
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;
}
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.......");
}
}
//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];
//Exit
private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}
ScreenShort: