0% found this document useful (0 votes)
20 views10 pages

Coding VP You

Uploaded by

eymann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Coding VP You

Uploaded by

eymann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace projectVp

public partial class Form1 : Form

private string connectionString = @"Data Source=LAPTOP-E12PJ6V3\SQLEXPRESS;Initial


Catalog=dealershipDB;Integrated Security=True";

public Form1()

InitializeComponent();

private void Form1_Load(object sender, EventArgs e)

LoadData();

private void LoadData()

try

using (SqlConnection connection = new SqlConnection(connectionString))


{

connection.Open();

string query = "SELECT * FROM Cars";

SqlCommand command = new SqlCommand(query, connection);

SqlDataAdapter adapter = new SqlDataAdapter(command);

DataTable dataTable = new DataTable();

adapter.Fill(dataTable);

dgv1.DataSource = dataTable;

cbxLocation.Items.Clear();

cbxLocation.Items.Add("Kepong");

cbxLocation.Items.Add("Segambut");

cbxSearchVehicleNumber.Items.Clear();

LoadVehicleNumbers();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void LoadVehicleNumbers()

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();
string query = "SELECT VehicleNumber FROM Cars";

SqlCommand command = new SqlCommand(query, connection);

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())

cbxSearchVehicleNumber.Items.Add(reader["VehicleNumber"].ToString());

reader.Close();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void cbxSearchVehicleNumber_SelectedIndexChanged(object sender, EventArgs e)

string selectedVehicleNumber = cbxSearchVehicleNumber.SelectedItem.ToString();

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

string query = "SELECT * FROM Cars WHERE VehicleNumber = @VehicleNumber";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleNumber", selectedVehicleNumber);
SqlDataReader reader = command.ExecuteReader();

if (reader.Read())

txtVehicleNumber.Text = reader["VehicleNumber"].ToString();

txtVehicleMake.Text = reader["VehicleMake"].ToString();

txtSellingPrice.Text = reader["SellingPrice"].ToString();

cbxLocation.SelectedItem = reader["Location"].ToString();

reader.Close();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void ClearForm()

txtVehicleNumber.Text = string.Empty;

txtVehicleMake.Text = string.Empty;

txtSellingPrice.Text = string.Empty;

cbxLocation.SelectedItem = null;

private void btnAdd_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))


{

connection.Open();

string query = "INSERT INTO Cars (VehicleNumber, VehicleMake, SellingPrice, Location) " +

"VALUES (@VehicleNumber, @VehicleMake, @SellingPrice, @Location)";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleNumber", txtVehicleNumber.Text);

command.Parameters.AddWithValue("@VehicleMake", txtVehicleMake.Text);

command.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);

command.Parameters.AddWithValue("@Location", cbxLocation.SelectedItem.ToString());

command.ExecuteNonQuery();

LoadData();

ClearForm();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void btnUpdate_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();
string query = "UPDATE Cars SET VehicleMake = @VehicleMake, SellingPrice =
@SellingPrice, " +

"Location = @Location WHERE VehicleNumber = @VehicleNumber";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleNumber", txtVehicleNumber.Text);

command.Parameters.AddWithValue("@VehicleMake", txtVehicleMake.Text);

command.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);

command.Parameters.AddWithValue("@Location", cbxLocation.SelectedItem.ToString());

command.ExecuteNonQuery();

LoadData();

ClearForm();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void btnDelete_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

string query = "DELETE FROM Cars WHERE VehicleNumber = @VehicleNumber";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleNumber", txtVehicleNumber.Text);
command.ExecuteNonQuery();

LoadData();

ClearForm();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void btnSearch_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

string query = "SELECT Location FROM Cars WHERE VehicleNumber = @VehicleNumber";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleNumber", txtSearchVehicleNumber.Text);

object result = command.ExecuteScalar();

if (result != null)

MessageBox.Show("Vehicle location: " + result.ToString());

else

{
MessageBox.Show("Vehicle not found.");

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void btnSearchMake_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

string query = "SELECT VehicleNumber, Location FROM Cars WHERE VehicleMake =


@VehicleMake";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@VehicleMake", txtSearchMake.Text);

SqlDataAdapter adapter = new SqlDataAdapter(command);

DataTable dataTable = new DataTable();

adapter.Fill(dataTable);

if (dataTable.Rows.Count == 0)

MessageBox.Show("No cars found for the specified make.");

return;

}
StringBuilder message = new StringBuilder();

message.AppendLine($"Cars of make '{txtSearchMake.Text}' listed by branch:");

foreach (DataRow row in dataTable.Rows)

string vehicleNumber = row["VehicleNumber"].ToString();

string location = row["Location"].ToString();

message.AppendLine($"Vehicle Number: {vehicleNumber}, Branch: {location}");

MessageBox.Show(message.ToString());

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

private void btnReport_Click(object sender, EventArgs e)

try

using (SqlConnection connection = new SqlConnection(connectionString))

connection.Open();

string query = "SELECT VehicleNumber, VehicleMake, SellingPrice, Location FROM Cars";

SqlCommand command = new SqlCommand(query, connection);

SqlDataAdapter adapter = new SqlDataAdapter(command);

DataTable dataTable = new DataTable();

adapter.Fill(dataTable);
ReportForm reportForm = new ReportForm(dataTable);

reportForm.Show();

catch (Exception ex)

MessageBox.Show("An error occurred: " + ex.Message);

You might also like