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

Assignment 03_047

Uploaded by

Sundas Shafi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 03_047

Uploaded by

Sundas Shafi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Visual Programming

Assignment No.03

Name: Sundas Shafi


Registration #: F20605047
Semester: 7th

Date: Nov 18th, 2023

Submitted To: Lec Zohaib Shah

COMPUTER SCIENCE DEPARTMENT


User Interface Code

Xaml Code
<Window x:Class="Assignment_03.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:Assignment_03"
mc:Ignorable="d"
Title="Sign-up form" Height="600" Width="500"
WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Label Content="Sign Up Form" HorizontalAlignment="Center" Margin="0,50,0,0"
VerticalAlignment="Top" Background="Transparent" Foreground="#FF090000" FontWeight="Bold"
FontSize="32" RenderTransformOrigin="0.737,0.377"/>
<Label Content="Name" HorizontalAlignment="Left" Margin="83,160,0,0" VerticalAlignment="Top"
RenderTransformOrigin="0.436,6.561" FontSize="24"/>
<Label Content="Father Name" HorizontalAlignment="Left" Margin="83,228,0,0"
VerticalAlignment="Top" FontSize="24"/>
<Label Content="Email" HorizontalAlignment="Left" Margin="83,292,0,0"
VerticalAlignment="Top" FontSize="24"/>
<Label Content="Contact" HorizontalAlignment="Left" Margin="83,363,0,0"
VerticalAlignment="Top" FontSize="24"/>
<TextBox x:Name="tbName" HorizontalAlignment="Left" Margin="275,168,0,0" TextWrapping="Wrap"
VerticalAlignment="Top" Width="189" FontSize="24"/>
<TextBox x:Name="tbFName" HorizontalAlignment="Left" Margin="275,231,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="189" FontSize="24"/>
<TextBox x:Name="tbEmail" HorizontalAlignment="Left" Margin="275,304,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="189" FontSize="24"/>
<TextBox x:Name="tbContact" HorizontalAlignment="Left" Margin="275,371,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="189" FontSize="24"/>
<Button Content="Clear" HorizontalAlignment="Left" Margin="89,460,0,0"
VerticalAlignment="Top" TextBlock.FontSize="24" TextBlock.FontFamily="sans-serif" Padding="5"
Background="DarkGray" Click="Button_Click" Width="86"/>
<Button Content="Add" HorizontalAlignment="Left" Margin="326,460,0,0"
VerticalAlignment="Top" TextBlock.FontSize="24" TextBlock.FontFamily="sans-serif" Padding="5"
Background="DarkGray" Click="Button_Click1" Width="86"/>
</Grid>
</Window>
Output

C# Code
Code C#
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;
using System.Xml.Linq;
namespace Assignment_03
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
SqlConnection sqlcon = new SqlConnection("Data Source=DESKTOP-MUTDHN7\\SQLEXPRESS;Initial
Catalog=master;Integrated Security=True");

DataSet ds = new DataSet();

//functions to clear buttons


public void ClearText()
{
tbName.Clear();
tbFName.Clear();
tbEmail.Clear();
tbContact.Clear();
}

// Button logic to clear textboxes


private void Button_Click(object sender, RoutedEventArgs e)
{
ClearText();
}

// Validation checks
private bool ValidateName()
{
string name = tbName.Text;
if (string.IsNullOrWhiteSpace(name))
{
return true;
}
else
return false;
}
private bool ValidateFName()
{
string fName = tbFName.Text;
if (string.IsNullOrWhiteSpace(fName))
{
return true;
}
else
return false;
}
private bool ValidateEmail()
{
string email = tbEmail.Text;
if (string.IsNullOrWhiteSpace(email))
{
return true;
}
else
return false;
}

private bool ValidateContact()


{
string contactNo = tbContact.Text;
if (string.IsNullOrWhiteSpace(contactNo))
{
return true;
}
else
return false;
}
private void Button_Click1(object sender, RoutedEventArgs e)
{
try
{
// Call the validation functions before processing the form submission
if (ValidateName() == true)
{
MessageBox.Show("Name cannot be empty or null.", "Validation Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (ValidateEmail() == true)
{
MessageBox.Show("Father Name cannot be empty or null.", "Validation Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (ValidateFName() == true)
{
MessageBox.Show("Email cannot be empty or null.", "Validation Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
else if (ValidateContact() == true)
{
MessageBox.Show("Contact No cannot be empty or null.", "Validation Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
if (sqlcon.State != ConnectionState.Open)
{
sqlcon.Open();
}
if (!string.IsNullOrWhiteSpace(tbEmail.Text))
{

SqlCommand cmd = new SqlCommand("SELECT * FROM assign03 WHERE email = '" +


tbEmail.Text + "'", sqlcon);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//cmd.Parameters.AddWithValue("@email", tbEmail.Text);
da.Fill(ds);

int i = ds.Tables[0].Rows.Count;
if (i > 0)
{
MessageBox.Show("Email : " + tbEmail.Text + " Already Exists");
ds.Clear();
}
else
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO assign03 VALUES
(@name,@fname,@contact,@email)", sqlcon);
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("@name", tbName.Text);
cmd1.Parameters.AddWithValue("@fname", tbEmail.Text);
cmd1.Parameters.AddWithValue("@email", tbFName.Text);
cmd1.Parameters.AddWithValue("@contact", tbContact.Text);
cmd1.ExecuteNonQuery();
sqlcon.Close();
MessageBox.Show("User added Successfully.", "saved",
MessageBoxButton.OK, MessageBoxImage.Information);
ClearText();
}

}
else
{
MessageBox.Show("Please fill in all the fields and accept the terms and
conditions.", "Validation Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// Finally block will execute this code no matter the exception.
finally
{
sqlcon.Close();
}
}
}
}

Validation Checks

For Father Name For User Name

For Email for Contact No


Clear Button functionaliy Check

Email Already Added Functionality Check


Database Connections:
➢ Creating Table with Email and Password Values:

➢ Table Values:

➢ Connection Test:
➢ Database Connection:

You might also like