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

Data Base Connectivity Code

The document contains code for a banking management system application. It includes classes for database connectivity, login authentication, home and employee dashboard screens. Functionality allows employees to manage other employees, customers, and perform login/logout. The code handles form navigation, data retrieval from repositories, validation, and basic CRUD operations.

Uploaded by

Rabia kainat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Data Base Connectivity Code

The document contains code for a banking management system application. It includes classes for database connectivity, login authentication, home and employee dashboard screens. Functionality allows employees to manage other employees, customers, and perform login/logout. The code handles form navigation, data retrieval from repositories, validation, and basic CRUD operations.

Uploaded by

Rabia kainat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

//data base connectivity code

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Repository
{
public class DatabaseConnectionClass
{
private SqlConnection myConnection;
private SqlCommand myCommand;

public DatabaseConnectionClass()
{
string connectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial
Catalog=BankingManagmentSystem;Data Source=RABIAKAINAT
";
myConnection = new SqlConnection(connectionString);
}

public void ConnectWithDB()


{
myConnection.Open();
}
public void CloseConnection()
{
myConnection.Close();
}

public SqlDataReader GetData(string query)


{
myCommand = new SqlCommand(query, myConnection);
//SqlDataReader sdr = myCommand.ExecuteReader();
//return sdr;
return myCommand.ExecuteReader();
}
public int ExecuteSQL(string query)
{
myCommand = new SqlCommand(query, myConnection);
//int x= myCommand.ExecuteNonQuery();
//return x;
return myCommand.ExecuteNonQuery();
}
}

}
using Entity;
using Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
public partial class LoginForm : Form
{
internal LoginRepo lr;
public LoginForm()
{
InitializeComponent();

lr = new LoginRepo();
}

private void LoginBtn_Click(object sender, EventArgs e)


{
string id = UserTB.Text;
string password = PassTB.Text;
Login l = lr.GetUser(id, password);

if (l != null)
{
HomePage hp = new HomePage(l);
this.Visible = false;
hp.Visible = true;
HomePage.identity = id;
}
else
{
MessageBox.Show("Invalid Id or Password");
}
}

private void ExitBtn_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void OnFormClose(object sender, FormClosedEventArgs e)


{
Application.Exit();
}

private void UserTB_TextChanged(object sender, EventArgs e)


{

private void PassTB_TextChanged(object sender, EventArgs e)


{
}
}
}

using Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
public partial class HomePage : Form
{
public static string identity;
Login l=new Login();
public HomePage(Login l)
{
InitializeComponent();
this.l = l;
WelcomeLabel.Text += l.Id;
}

private void OnFormClose(object sender, FormClosedEventArgs e)


{
Application.Exit();
}

private void ManageEmpBtn_Click(object sender, EventArgs e)


{
if (l.Role == 0)
{
ManageEmployeeForm mef = new ManageEmployeeForm(l);
this.Visible = false;
mef.Visible = true;
}
else
{
MessageBox.Show("Access Denied");
}
}

private void button1_Click(object sender, EventArgs e)


{

if (l.Role == 1)
{
EmployeeActivities ca = new EmployeeActivities();
this.Visible = false;
ca.Visible = true;
}
else
{
MessageBox.Show("Access Denied");
}
}

private void LogoutBtn_Click(object sender, EventArgs e)


{
LoginForm lf = new LoginForm();
lf.Visible = true;
this.Visible = false;
}

private void ChangePassBtn_Click(object sender, EventArgs e)


{
ChangePass cg = new ChangePass(this);
this.Visible = false;
cg.Visible = true;

}
public void test()
{
this.Visible = true;

}
}

using Entity;
using Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
public partial class ManageCustomerForm : Form
{
Login l;
CustomerRepo cr;
LoginRepo lr;
public ManageCustomerForm(Login l)
{
InitializeComponent();
this.l = l;
cr = new CustomerRepo();
lr = new LoginRepo();
}
private void OnFormClose(object sender, FormClosedEventArgs e)
{
Application.Exit();
}

private void LoadBtnn_Click_1(object sender, EventArgs e)


{
string custId = CustIdTF.Text;

Customer cust = cr.GetCustomer(custId);

if (cust == null)
{
MessageBox.Show("Invalid Id");
this.RefreshBtnn_Click_1(sender, e);
}
else
{
this.CustNameTB.Text = cust.Name;
this.CustPhnNumberTB2.Text = cust.PhnNumber.Substring(4);

this.RefreshBtnn.Enabled = true;
this.LoadBtnn.Enabled = false;
this.InsertBtnn.Enabled = false;
this.UpdateBtnn.Enabled = true;
this.DeleteBtnn.Enabled = true;

this.CustIdTF.Enabled = false;
}

private void InsertBtnn_Click_1(object sender, EventArgs e)


{
Login l = new Login();
Customer cust = new Customer();
try
{
l.Id = this.CustIdTF.Text;
cust.CustId = this.CustIdTF.Text;
cust.Name = this.CustNameTB.Text;
int phn = Convert.ToInt32(this.CustPhnNumberTB2.Text);
cust.PhnNumber = this.CustPhnNumberTB1.Text + this.CustPhnNumberTB2.Text;

if (lr.InsertUser(l))
{
if (cr.InsertCustomer(cust))
{
MessageBox.Show("Customer Added. Id : " + cust.CustId);
this.RefreshBtnn_Click_1(sender, e);
this.ViewAllBtnn_Click_1(sender, e);
}
}
else
{
MessageBox.Show("Can Not Add" + l.Id);
}
}
catch (Exception ex)
{
MessageBox.Show("Invalid Data");
}

private void ViewAllBtnn_Click_1(object sender, EventArgs e)


{
List<Customer> listOfCustomer = cr.GetAllCustomers();
this.CustTable.DataSource = listOfCustomer;
}

private void UpdateBtnn_Click_1(object sender, EventArgs e)


{
Customer cust = new Customer();
cust.CustId = this.CustIdTF.Text;
cust.Name = this.CustNameTB.Text;
int phn = Convert.ToInt32(this.CustPhnNumberTB2.Text);
cust.PhnNumber = this.CustPhnNumberTB1.Text + this.CustPhnNumberTB2.Text;

if (cr.UpdateCustomer(cust))
{
MessageBox.Show("Update Done");
this.RefreshBtnn_Click_1(sender, e);
this.ViewAllBtnn_Click_1(sender, e);
}
else
{
MessageBox.Show("Update NOT Done");
}

private void DeleteBtnn_Click_1(object sender, EventArgs e)


{
Login l = new Login();
l.Id = this.CustIdTF.Text;
Customer cust = new Customer();
cust.CustId = this.CustIdTF.Text;

if (lr.DeleteUser(l))
{
if (cr.DeleteCustomer(cust))
{
MessageBox.Show("Deleted");
this.RefreshBtnn_Click_1(sender, e);
this.ViewAllBtnn_Click_1(sender, e);
}
}
else
{
MessageBox.Show("Can Not Delete");
}
}

private void RefreshBtnn_Click_1(object sender, EventArgs e)


{
this.CustIdTF.Text = "";
this.CustNameTB.Text = "";
this.CustPhnNumberTB2.Text = "";

this.RefreshBtnn.Enabled = false;
this.LoadBtnn.Enabled = true;
this.InsertBtnn.Enabled = true;
this.UpdateBtnn.Enabled = false;
this.DeleteBtnn.Enabled = false;

this.CustIdTF.Enabled = true;
}

private void SearchBoxx_TextChanged(object sender, EventArgs e)


{
List<Customer> listOfCustomer = cr.GetAllCustomers();
string keyword = SearchBoxx.Text.ToLower();
List<Customer> searchedList = listOfCustomer.FindAll(x => ((x.CustId.ToLower()).Contains(keyword)) ||
((x.Name.ToLower()).Contains(keyword)) || ((x.PhnNumber.ToLower()).Contains(keyword)));

CustTable.DataSource = searchedList;
}

private void CustTable_CellContentClick(object sender, DataGridViewCellEventArgs e)


{
this.CustIdTF.Text = CustTable.Rows[e.RowIndex].Cells[0].Value.ToString();
this.CustNameTB.Text = CustTable.Rows[e.RowIndex].Cells[1].Value.ToString();
this.CustPhnNumberTB2.Text = (CustTable.Rows[e.RowIndex].Cells[2].Value.ToString()).Substring(4);

this.RefreshBtnn.Enabled = true;
this.LoadBtnn.Enabled = false;
this.InsertBtnn.Enabled = false;
this.UpdateBtnn.Enabled = true;
this.DeleteBtnn.Enabled = true;

this.CustIdTF.Enabled = true;

private void LogoutBtn_Click(object sender, EventArgs e)


{
LoginForm lf = new LoginForm();
lf.Visible = true;
this.Visible = false;
}

private void BackBtn_Click(object sender, EventArgs e)


{
EmployeeActivities ea=new EmployeeActivities();
this.Visible=false;
ea.Visible=true;
}

private void ManageCustomerForm_Load(object sender, EventArgs e)


{

private void EmpPhnNumberLabel_Click(object sender, EventArgs e)


{

}
}
}

using Entity;
using Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App
{
public partial class ManageEmployeeForm : Form
{
Login l;
EmployeeRepo er;
LoginRepo lr;
public ManageEmployeeForm(Login l)
{
InitializeComponent();
this.l = l;
er = new EmployeeRepo();
lr = new LoginRepo();
}

private void OnFormClose(object sender, FormClosedEventArgs e)


{
Application.Exit();
}

private void LoadBtn_Click(object sender, EventArgs e)


{
string empId = EmpIdTF.Text;

Employee emp = er.GetEmployee(empId);

if (emp == null)
{
MessageBox.Show("Invalid Id");
this.RefreshBtn_Click(sender, e);
}
else
{
this.EmpNameTB.Text = emp.Name;
this.EmpPhnNumberTB2.Text = emp.PhnNumber.Substring(4);
this.EmpSalaryTB.Text = emp.Salary + "";
this.EmpDesignationTB.Text = emp.Designation;

this.RefreshBtn.Enabled = true;
this.LoadBtn.Enabled = false;
this.InsertBtn.Enabled = false;
this.UpdateBtn.Enabled = true;
this.DeleteBtn.Enabled = true;

this.EmpIdTF.Enabled = false;
}

private void ViewAllBtn_Click(object sender, EventArgs e)


{
List<Employee> listOfEmployee = er.GetAllEmployees();
this.EmpTable.DataSource = listOfEmployee;
}

private void InsertBtn_Click(object sender, EventArgs e)


{
Login l = new Login();
Employee emp = new Employee();
int p = new Random().Next(99999999)+10000000;
try
{
l.Id = this.EmpIdTF.Text;
l.Password = p + "";

emp.EmpId = this.EmpIdTF.Text;
emp.Name = this.EmpNameTB.Text;
int phn = Convert.ToInt32(this.EmpPhnNumberTB2.Text);
emp.PhnNumber = this.EmpPhnNumberTB1.Text + this.EmpPhnNumberTB2.Text;
double sal = Convert.ToDouble(this.EmpSalaryTB.Text);
emp.Salary = sal;
emp.Designation = this.EmpDesignationTB.Text;

if((emp.Designation.ToLower()).Equals("manager"))
{
l.Role = 0;
}
else
{
l.Role = 1;
}

if (lr.InsertUser(l))
{
if (er.InsertEmployee(emp))
{
MessageBox.Show("Employee Added. Id : " + emp.EmpId + " & Password : " + l.Password);
this.RefreshBtn_Click(sender, e);
this.ViewAllBtn_Click(sender, e);
}
}
else
{
MessageBox.Show("Can Not Add" + l.Id);
}
}
catch (Exception exp)
{
MessageBox.Show("Invalid Data");
}
}

private void RefreshBtn_Click(object sender, EventArgs e)


{
this.EmpIdTF.Text = "";
this.EmpNameTB.Text = "";
this.EmpPhnNumberTB2.Text = "";
this.EmpSalaryTB.Text = "";
this.EmpDesignationTB.Text = "";

this.RefreshBtn.Enabled = false;
this.LoadBtn.Enabled = true;
this.InsertBtn.Enabled = true;
this.UpdateBtn.Enabled = false;
this.DeleteBtn.Enabled = false;

this.EmpIdTF.Enabled = true;
}

private void DeleteBtn_Click(object sender, EventArgs e)


{
Login l = new Login();
l.Id = this.EmpIdTF.Text;
Employee emp = new Employee();
emp.EmpId = this.EmpIdTF.Text;

if (lr.DeleteUser(l))
{
if (er.DeleteEmployee(emp))
{
MessageBox.Show("Deleted");
this.RefreshBtn_Click(sender, e);
this.ViewAllBtn_Click(sender, e);
}
}
else
{
MessageBox.Show("Can Not Delete");
}
}

private void UpdateBtn_Click(object sender, EventArgs e)


{
Employee emp = new Employee();
emp.EmpId = this.EmpIdTF.Text;
emp.Name = this.EmpNameTB.Text;
int phn = Convert.ToInt32(this.EmpPhnNumberTB2.Text);
emp.PhnNumber = this.EmpPhnNumberTB1.Text + this.EmpPhnNumberTB2.Text;
double sal = Convert.ToDouble(this.EmpSalaryTB.Text);
emp.Salary = sal;
emp.Designation = this.EmpDesignationTB.Text;

if (er.UpdateEmployee(emp))
{
MessageBox.Show("Update Done");
this.RefreshBtn_Click(sender, e);
this.ViewAllBtn_Click(sender, e);
}
else
{
MessageBox.Show("Update NOT Done");
}
}

private void EmpTableCellClicked(object sender, DataGridViewCellEventArgs e)


{
this.EmpIdTF.Text = EmpTable.Rows[e.RowIndex].Cells[0].Value.ToString();
this.EmpNameTB.Text = EmpTable.Rows[e.RowIndex].Cells[1].Value.ToString();
this.EmpPhnNumberTB2.Text = (EmpTable.Rows[e.RowIndex].Cells[2].Value.ToString()).Substring(4);
this.EmpSalaryTB.Text = EmpTable.Rows[e.RowIndex].Cells[3].Value.ToString();
this.EmpDesignationTB.Text = EmpTable.Rows[e.RowIndex].Cells[4].Value.ToString();

this.RefreshBtn.Enabled = true;
this.LoadBtn.Enabled = false;
this.InsertBtn.Enabled = false;
this.UpdateBtn.Enabled = true;
this.DeleteBtn.Enabled = true;

this.EmpIdTF.Enabled = false;

private void SearchTBValueChanged(object sender, EventArgs e)


{
List<Employee> listOfEmployee = er.GetAllEmployees();
string keyword = SearchBox.Text.ToLower();
List<Employee> searchedList = listOfEmployee.FindAll(x => ((x.EmpId.ToLower()).Contains(keyword)) ||
((x.Name.ToLower()).Contains(keyword)) || ((x.PhnNumber.ToLower()).Contains(keyword)) ||
((x.Designation.ToLower()).Contains(keyword)));

EmpTable.DataSource = searchedList;

private void LogoutBtn_Click(object sender, EventArgs e)


{
LoginForm lf = new LoginForm();
lf.Visible = true;
this.Visible = false;
}

private void BackBtn_Click(object sender, EventArgs e)


{
HomePage hp = new HomePage(l);
hp.Visible = true;
this.Visible = false;
}

private void EmpTable_CellContentClick(object sender, DataGridViewCellEventArgs e)


{

}
}
}

You might also like