c#labsheet3 dipendra
c#labsheet3 dipendra
Report on:
C# labsheet 3
namespace lab3_qn1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
}
OUTPUT:
2. Use visual studio and the forms designer to build a simple
application where you could add or delete friends to a list.
using System;
using System.Collections;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace lab3_q2
{
public partial class Form1 : Form
{
List<string> list = new List<string>() ;
public Form1()
{
InitializeComponent();
list.Add("Ram");
list.Add("hari");
list.Add("Rohan");
list.Add("suman");
list.Add("bishal");
list.Add("rahul");
listBox1.DataSource = list;
list.Add(newItem);
listBox1.DataSource = null;
listBox1.DataSource = list;
tb_add.Clear();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
listBox1.DataSource = null;
listBox1.DataSource = list;
listBox1.ClearSelected();
}
else if (listBox1.SelectedIndex != -1)
{
string selectedItem = listBox1.SelectedItem.ToString();
list.Remove(selectedItem);
listBox1.DataSource = null;
listBox1.DataSource = list;
listBox1.ClearSelected();
}
}
listBox1.DataSource = null;
listBox1.DataSource = new BindingSource(sortedList, null);
listBox1.DisplayMember = "Key";
}
else
{
listBox1.DataSource = null;
listBox1.DataSource = list;
}
}
}
}
OUTPUT:
Use visual studio and the forms designer to build an application
to accept employee’s detail and store the same in database. Also
include option for navigating through the database. Also include
features to modify/delete existing record from the database.
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace L3_No3
{
public partial class Form1 : Form
{
private String connectionString = "Data Source=ABHI-XTHA\\SQLEXPRESS;Initial
Catalog=Labsheet3_No3;Persist Security Info=True;User
ID = sa; Password=abhishek;Encrypt=False";
private int currentEmpId = 0;
public Form1()
{
InitializeComponent();
init();
}
private void PageNo()
{
tb_pageno.Text = "Page No " + currentEmpId.ToString() + " Out of " +
EmpNum();
}
private void btn_close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void init()
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
int totalEmployees = EmpNum();
string query = "SELECT * FROM Employees WHERE EmpId=@EmpId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
currentEmpId++;
if (currentEmpId > totalEmployees)
{
currentEmpId = 1;
}
cmd.Parameters.AddWithValue("@EmpId", currentEmpId);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
tb_firstname.Text = reader["FirstName"].ToString();
tb_surname.Text = reader["Surname"].ToString();
tb_jtitle.Text = reader["JobTitle"].ToString();
tb_department.Text = reader["Department"].ToString();
}
reader.Close();
}
con.Close();
PageNo();
}
}
public int EmpNum()
{
int empnum = 0;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
string query = "SELECT COUNT(EmpID) AS Number FROM Employees";
using (SqlCommand cmd = new SqlCommand(query, con))
{
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
empnum = Convert.ToInt32(result);
}
}
con.Close();
}
return empnum;
}
private void btn_prev_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
int totalEmployees = EmpNum();
string query = "SELECT * FROM Employees WHERE EmpId=@EmpId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
currentEmpId--;
if (currentEmpId <= 0)
{
currentEmpId = totalEmployees;
}
cmd.Parameters.AddWithValue("@EmpId", currentEmpId);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
tb_firstname.Text = reader["FirstName"].ToString();
tb_surname.Text = reader["Surname"].ToString();
tb_jtitle.Text = reader["JobTitle"].ToString();
tb_department.Text = reader["Department"].ToString();
}
reader.Close();
}
con.Close();
PageNo();
}
}
private void btn_next_Click(object sender, EventArgs e)
{
init();
}
private void btn_firstrec_Click(object sender, EventArgs e)
{
currentEmpId = 0;
init();
}
private void btn_lastrec_Click(object sender, EventArgs e)
{
currentEmpId = EmpNum() - 1;
init();
}
private void btn_addnew_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
con.Open();
string query = "INSERT INTO Employees(FirstName, Surname,
JobTitle, Department) "
+
"VALUES (@FirstName, @Surname, @JobTitle, @Department)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@FirstName",
tb_firstname.Text);
cmd.Parameters.AddWithValue("@Surname", tb_surname.Text);
cmd.Parameters.AddWithValue("@JobTitle", tb_jtitle.Text);
cmd.Parameters.AddWithValue("@Department",
tb_department.Text);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Employee added successfully!");
}
else
{
MessageBox.Show("Failed to add employee.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
con.Close();
}
PageNo();
}
}
private void btn_update_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
con.Open();
string query = "UPDATE Employees SET FirstName=@FirstName,
Surname=@Surname,
JobTitle = @JobTitle, Department = @Department " +
"WHERE EmpId=@EmpId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@FirstName",
tb_firstname.Text);
cmd.Parameters.AddWithValue("@Surname", tb_surname.Text);
cmd.Parameters.AddWithValue("@JobTitle", tb_jtitle.Text);
cmd.Parameters.AddWithValue("@Department",
tb_department.Text);
cmd.Parameters.AddWithValue("@EmpId", currentEmpId);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Employee updated successfully!");
}
else
{
MessageBox.Show("Failed to update employee.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
con.Close();
}
PageNo();
}
}
private void btn_delete_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
con.Open();
string query = "DELETE FROM Employees WHERE EmpId=@EmpId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@EmpId", currentEmpId);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Employee deleted successfully!");
currentEmpId++;
}
else
{
MessageBox.Show("Failed to delete employee.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
con.Close();
}
init();
}
}
}
}
Conclusion
We learned about the .Net Framework windows application in this lab sheet. Event oriented
programming was emphasized and GUI programming was learned using MS Visual Studio’s drag and
drop UI maker.