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

Assignment#3

Assignment of Software engineering

Uploaded by

Malik Zohaib
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)
14 views

Assignment#3

Assignment of Software engineering

Uploaded by

Malik Zohaib
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/ 10

VISUAL PROGRAMMING

ASSIGNMENT # 3
Simple Contact Management System

DATE: 08-06-2023

CLASS: BSCS-5

SUBMITTED BY
Saad Ahmed Khan (212202002)

SUBMITTED TO
Sir Fahad Akbar
CODING
.xaml file
<Window x:Class="PhoneBookApplication.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"
xmlns:local="clr-namespace:PhoneBookApplication"
mc:Ignorable="d"
Title="Phone Book" Height="450" Width="800" x:Name="PhoneBook"
Loaded="PhoneBook_Loaded">
<Grid Background="AliceBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="620*"/>
<ColumnDefinition Width="167*"/>
<ColumnDefinition Width="13*"/>
</Grid.ColumnDefinitions>
<Label Content="Phone Book Application" FontWeight="Bold"
FontSize="18" HorizontalAlignment="Left" Margin="305,30,0,0"
Name="label1" VerticalAlignment="Top" />
<Label Content="Name" FontWeight="Bold" FontSize="14"
HorizontalAlignment="Left" Margin="84,126,0,0" Name="label2"
VerticalAlignment="Top" />
<Label Content="E-Mail" FontWeight="Bold" FontSize="14"
HorizontalAlignment="Left" Margin="84,173,0,0" Name="label3"
VerticalAlignment="Top" />
<Label Content="Mobile Number" FontWeight="Bold" FontSize="14"
HorizontalAlignment="Left" Margin="84,217,0,0" VerticalAlignment="Top"
/>
<TextBox HorizontalAlignment="Left" Margin="221,130,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
Name="textName" TextChanged="textName_TextChanged"/>
<TextBox HorizontalAlignment="Left" Margin="221,177,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
Name="textEmail" />
<TextBox HorizontalAlignment="Left" Margin="221,222,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="120"
Name="textNumber"/>
<Button Content="Insert" Background="LightGreen" FontWeight="Bold"
FontSize="14" HorizontalAlignment="Left" Margin="84,300,0,0"
VerticalAlignment="Top" Name="InsertButton" Width="53"
Click="InsertButton_Click"/>
<Button Content="Update" Background="GreenYellow"
FontWeight="Bold" FontSize="14" HorizontalAlignment="Left"
Margin="192,300,0,0" VerticalAlignment="Top" Name="UpdateButton"
Width="53" Click="UpdateButton_Click"/>
<Button Content="Delete" Background="OrangeRed" FontWeight="Bold"
FontSize="14" HorizontalAlignment="Left" Margin="290,300,0,0"
VerticalAlignment="Top" Name="DeleteButton" Width="53"
Click="DeleteButton_Click"/>
<DataGrid AutoGenerateColumns="True" Margin="369,130,5,94"
Name="GridView" SelectionChanged="GridView_SelectionChanged"
Grid.ColumnSpan="2"/>
<Label Content="Search" FontWeight="Bold" FontSize="14"
HorizontalAlignment="Left" Margin="369,90,0,0" Name="label4"
VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Margin="432,95,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="272"
Name="textSearch" Grid.ColumnSpan="2"
TextChanged="textSearch_TextChanged"/>
<Button Content="Search" Background="LightSkyBlue"
FontWeight="Bold" FontSize="14" HorizontalAlignment="Left"
Margin="104,90,0,0" VerticalAlignment="Top"
x:Name="UpdateButton_Copy" Width="53" Click="UpdateButton_Click"
Grid.Column="1"/>
</Grid>
</Window>
.xaml.cs file
using System.Windows;
using System.Windows.Controls;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;

namespace PhoneBookApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SqlConnection con = new SqlConnection("Data
Source=SaadAhmed\\SQLEXPRESS;Initial
Catalog=ELECTORALSYSTEM;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter data;
DataTable table;
private void clear()
{
textName.Text = " ";
textEmail.Text = " ";
textNumber.Text = " ";
}
private void show()
{
data = new SqlDataAdapter("select * from contact", con);
table = new DataTable();
data.Fill(table);
GridView.ItemsSource = table.DefaultView;
textName.Focus();
}

private bool ValidName(string n)


{
Regex check = new Regex(@"^([A-Z][a-z-A-z]+)$");
bool valid = false;
valid = check.IsMatch(n);
if (valid==true) {
return valid;
}
else
{
MessageBox.Show("Enter valid name");
return false;
}
}

private bool ValidPhone(string p)


{
Regex check = new Regex(@"^([0-9]+)$");
bool valid = false;
valid = check.IsMatch(p);
if (valid == true)
{
return valid;
}
else
{
MessageBox.Show("Enter a valid phone number");
return false;
}
}

private bool ValidEmail(string e)


{
Regex check = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
bool valid = false;
valid = check.IsMatch(e);
if (valid == true)
{
return valid;
}
else
{
MessageBox.Show("Enter a valid email address");
return false;
}
}

private void TextBox_TextChanged(object sender,


TextChangedEventArgs e)
{

private void PhoneBook_Loaded(object sender, RoutedEventArgs e)


{
show();
}

private void InsertButton_Click(object sender, RoutedEventArgs e)


{
bool name = ValidName(textName.Text);
bool phone = ValidPhone(textNumber.Text);
bool email = ValidEmail(textEmail.Text);
if (name == true && phone == true && email == true)
{
cmd = new SqlCommand("insert into contact(name, phone_number,
email) values(@name, @phoneNumber, @email)", con);
cmd.Parameters.AddWithValue("@name", textName.Text);
cmd.Parameters.AddWithValue("@phoneNumber",
textNumber.Text);
cmd.Parameters.AddWithValue("@email", textEmail.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Contact added successfully");
show();
clear();
}
}

private void UpdateButton_Click(object sender, RoutedEventArgs e)


{
cmd = new SqlCommand("update contact set
phone_number=@phoneNumber, email=@email where name=@name", con);
cmd.Parameters.AddWithValue("@name", textName.Text);
cmd.Parameters.AddWithValue("@phoneNumber", textNumber.Text);
cmd.Parameters.AddWithValue("@email", textEmail.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Contact updated successfully");
show();
clear();
}

private void DeleteButton_Click(object sender, RoutedEventArgs e)


{
cmd = new SqlCommand("delete from contact where name=@name",
con);
cmd.Parameters.AddWithValue("@name", textName.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Contact deleted successfully");
show();
clear();
}

private void GridView_SelectionChanged(object sender,


SelectionChangedEventArgs e)
{
try
{
if(GridView.Items.Count > 0)
{
textName.Text =
((DataRowView)GridView.SelectedItem).Row["name"].ToString();
textNumber.Text =
((DataRowView)GridView.SelectedItem).Row["number"].ToString();
textEmail.Text =
((DataRowView)GridView.SelectedItem).Row["email"].ToString();
}
}
catch { }
}

You might also like