0% found this document useful (0 votes)
17 views4 pages

Correction TP2 WPF CRUD

The document details code for a C# WPF application that performs CRUD operations on a SQL database table. It includes code to load, insert, update, delete and clear data from a data grid displaying the table contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Correction TP2 WPF CRUD

The document details code for a C# WPF application that performs CRUD operations on a SQL database table. It includes code to load, insert, update, delete and clear data from a data grid displaying the table contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Fichier MainWindow.xaml.

cs

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

namespace CRUDWPF
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadGrid();
}

SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-QL7I3EC;Initial


Catalog=NewDB;Integrated Security=True");
public void clearData()
{
name_txt.Clear();
age_txt.Clear();
gender_txt.Clear();
city_txt.Clear();
search_txt.Clear();
}

public void LoadGrid()


{
SqlCommand cmd = new SqlCommand("select * from FirstTable", con);
DataTable dt = new DataTable();
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
dt.Load(sdr);
con.Close();
datagrid.ItemsSource = dt.DefaultView;

}
private void ClearDataBtn_Click(object sender, RoutedEventArgs e)
{
clearData();
}

public bool isValid()


{
if(name_txt.Text == string.Empty)
{
MessageBox.Show("Name is required", "Failed", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (age_txt.Text == string.Empty)
{
MessageBox.Show("Age is required", "Failed", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (gender_txt.Text == string.Empty)
{
MessageBox.Show("Gender is required", "Failed", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (city_txt.Text == string.Empty)
{
MessageBox.Show("City is required", "Failed", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
return true;
}

private void InsertBtn_Click(object sender, RoutedEventArgs e)


{
try
{
if (isValid())
{
SqlCommand cmd = new SqlCommand("INSERT INTO FirstTable VALUES (@Name, @Age,
@Gender, @City)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", name_txt.Text);
cmd.Parameters.AddWithValue("@Age", age_txt.Text);
cmd.Parameters.AddWithValue("@Gender", gender_txt.Text);
cmd.Parameters.AddWithValue("@City", city_txt.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
LoadGrid();
MessageBox.Show("Successfully registered", "Saved", MessageBoxButton.OK,
MessageBoxImage.Information);
clearData();
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}

private void DeleteBtn_Click(object sender, RoutedEventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("Delete from FirstTable where ID = " +search_txt.Text+ " ",
con);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Record has been deleted", "Deleted", MessageBoxButton.OK,
MessageBoxImage.Information);
con.Close();
clearData();
LoadGrid();
con.Close();
}
catch (SqlException ex)
{
MessageBox.Show("Not deleted" + ex.Message);
}
finally
{
con.Close();
}
}

private void UpdateBtn_Click(object sender, RoutedEventArgs e)


{
con.Open();
SqlCommand cmd = new SqlCommand("update FirstTable set Name = '"+name_txt.Text+"', Age =
'"+age_txt.Text+"', Gender = '"+gender_txt.Text+"', City = '"+city_txt.Text+"' where ID = '"+search_txt.Text+"' ",
con);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Record has been successfully updated", "Update", MessageBoxButton.OK,
MessageBoxImage.Information);
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
clearData();
LoadGrid();
}
}
}
}

Fichier MainWindow.xaml
<Window x:Class="CRUDWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="900">
<Grid>
<Label Content="CRUD Operation in C#" HorizontalAlignment="Center" Margin="0,10,0,0"
VerticalAlignment="Top" RenderTransformOrigin="0.393,-0.05" Height="31" FontSize="16"/>
<Label Content="Name" HorizontalAlignment="Left" Margin="66,60,0,0" VerticalAlignment="Top"
RenderTransformOrigin="-0.16,-2.36" Height="36" FontSize="14"/>
<Label Content="Age" HorizontalAlignment="Left" Margin="66,101,0,0" VerticalAlignment="Top"
Height="36" FontSize="14"/>
<Label Content="Gender" HorizontalAlignment="Left" Margin="66,143,0,0" VerticalAlignment="Top"
Height="36" FontSize="14"/>
<Label Content="City" HorizontalAlignment="Left" Margin="66,184,0,0" VerticalAlignment="Top"
Height="36" FontSize="14"/>
<TextBox x:Name="name_txt" HorizontalAlignment="Left" Margin="130,60,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="154" Height="32" FontSize="14"/>
<TextBox x:Name="age_txt" HorizontalAlignment="Left" Margin="130,101,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="154" Height="32" FontSize="14"/>
<TextBox x:Name="gender_txt" HorizontalAlignment="Left" Margin="130,143,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="155" Height="32" FontSize="14"/>
<TextBox x:Name="city_txt" HorizontalAlignment="Left" Margin="130,184,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="155" Height="32" FontSize="14"/>
<DataGrid x:Name="datagrid" d:ItemsSource="{d:SampleData ItemCount=5}" Margin="313,60,29,217"/>
<Button x:Name="InsertBtn" Content="Insert Record" HorizontalAlignment="Left" Margin="82,339,0,0"
VerticalAlignment="Top" Height="47" Width="101" FontSize="14" FontWeight="Bold"
Background="#FFB96868" BorderBrush="Black" Foreground="#FFF8F7F7" OpacityMask="Black"
Click="InsertBtn_Click"/>
<Button x:Name="UpdateBtn" Content="Update Record" HorizontalAlignment="Left" Margin="267,339,0,0"
VerticalAlignment="Top" Height="47" Width="101" FontSize="14" FontWeight="Bold"
Background="#FFB96868" BorderBrush="Black" Foreground="#FFF8F7F7" OpacityMask="Black"
Click="UpdateBtn_Click"/>
<Button x:Name="DeleteBtn" Content="Delete Record" HorizontalAlignment="Left" Margin="457,339,0,0"
VerticalAlignment="Top" Height="47" Width="101" FontSize="14" FontWeight="Bold"
Background="#FFB96868" BorderBrush="Black" Foreground="#FFF8F7F7" OpacityMask="Black"
Click="DeleteBtn_Click"/>
<Button x:Name="ClearDataBtn" Content="Clear Data" HorizontalAlignment="Left" Margin="652,339,0,0"
VerticalAlignment="Top" Height="47" Width="101" FontSize="14" FontWeight="Bold"
Background="#FFB96868" BorderBrush="Black" Foreground="#FFF8F7F7" OpacityMask="Black"
Click="ClearDataBtn_Click"/>
<Label Content="ID" HorizontalAlignment="Left" Margin="231,251,0,0" VerticalAlignment="Top"
Height="36" FontSize="14"/>
<TextBox x:Name="search_txt" HorizontalAlignment="Left" Margin="289,251,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="155" Height="32" FontSize="14"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="592,269,0,0" VerticalAlignment="Top"/>

</Grid>
</Window>

You might also like