C# Access Database Tutorials In Urdu (Code) - Employee Info Screen



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace DatabaseDemoWithAccessVC
{
public partial class EmployeeInfoForm : Form
{
public EmployeeInfoForm()
{
InitializeComponent();
}

private int employeeId = 0;
public int EmployeeId
{
get { return employeeId; }
set { employeeId = value; }
}

private bool isUpdate = false;
public bool IsUpdate
{
get { return isUpdate; }
set { isUpdate = value; }
}

private void CloseButton_Click(object sender, EventArgs e)
{
this.Close();
}

private void SaveUpdateButton_Click(object sender, EventArgs e)
{
if (IsValidated())
{
try
{
if (this.isUpdate)
{
UpdateRecord();

MessageBox.Show("Record is updated successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
SaveRecord();

MessageBox.Show("Record is saved successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

this.Close();
}
catch (ApplicationException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

private void UpdateRecord()
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "UPDATE Employees " +
"SET Name = @Name, Email = @Email, Phone = @Phone, Mobile = @Mobile WHERE ID = @EmployeeId;";

using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
{
con.Open();

cmd.Parameters.AddWithValue("@Name", NameTextBox.Text);
cmd.Parameters.AddWithValue("@Email", EmailTextBox.Text);
cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text);
cmd.Parameters.AddWithValue("@Mobile", MobileTextBox.Text);
cmd.Parameters.AddWithValue("@EmployeeId", this.EmployeeId);

cmd.ExecuteNonQuery();
}
}
}


private void SaveRecord()
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "INSERT INTO Employees (Name, Email, Phone, Mobile) VALUES (@Name, @Email, @Phone, @Mobile)";

using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
{
con.Open();

cmd.Parameters.AddWithValue("@Name", NameTextBox.Text);
cmd.Parameters.AddWithValue("@Email", EmailTextBox.Text);
cmd.Parameters.AddWithValue("@Phone", PhoneTextBox.Text);
cmd.Parameters.AddWithValue("@Mobile", MobileTextBox.Text);

cmd.ExecuteNonQuery();
}
}
}

private bool IsValidated()
{
if (NameTextBox.Text.Trim() == string.Empty)
{
MessageBox.Show("Name is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
NameTextBox.Focus();
return false;
}

if (EmailTextBox.Text.Trim() == string.Empty)
{
MessageBox.Show("Email is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
EmailTextBox.Focus();
return false;
}

return true;
}

private void EmployeeInfoForm_Load(object sender, EventArgs e)
{
if (this.IsUpdate)
{
DataTable dtEmployee = GetEmployeeInfoById();
DataRow row = dtEmployee.Rows[0];

NameTextBox.Text = row["Name"].ToString();
EmailTextBox.Text = row["Email"].ToString();
PhoneTextBox.Text = row["Phone"].ToString();
MobileTextBox.Text = row["Mobile"].ToString();
}
}

private DataTable GetEmployeeInfoById()
{
DataTable dtEmployeeInfo = new DataTable();

string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
string cmdString = "SELECT Name, Email, Phone, Mobile FROM Employees WHERE Id = @EmployeeId";

using (OleDbConnection con = new OleDbConnection(connString))
{
using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
{
con.Open();

cmd.Parameters.AddWithValue("@EmployeeId", this.EmployeeId);

OleDbDataReader reader = cmd.ExecuteReader();

dtEmployeeInfo.Load(reader);
}
}

return dtEmployeeInfo;
}
}
}

Comments