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

c#labsheet3 dipendra

The document is a report on C# labsheet 3 from Nepal Engineering College, detailing the creation of three applications using Visual Studio. The first application is a simple calculator for basic arithmetic operations, the second allows adding and deleting friends from a list, and the third manages employee records in a database with options for navigation, modification, and deletion. The report concludes with insights on learning .NET Framework and GUI programming through event-oriented programming techniques.

Uploaded by

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

c#labsheet3 dipendra

The document is a report on C# labsheet 3 from Nepal Engineering College, detailing the creation of three applications using Visual Studio. The first application is a simple calculator for basic arithmetic operations, the second allows adding and deleting friends from a list, and the third manages employee records in a database with options for navigation, modification, and deletion. The report concludes with insights on learning .NET Framework and GUI programming through event-oriented programming techniques.

Uploaded by

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

Nepal Engineering College

(Affiliated to Pokhara University)


Changunarayan, Bhaktapur

Report on:

C# labsheet 3

Submitted By: Submitted to:


Name: Kritesh Shrestha
Department of Computer
Roll no: 021-334
Engineering
1.Use visual studio and the forms designer to build a simple
calculator that performs addition, subtraction, multiplication
and division on floating point numbers.
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;

namespace lab3_qn1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void label2_Click(object sender, EventArgs e)


{

private void btn_add_Click(object sender, EventArgs e)


{
double a, b,sum;
a = Convert.ToDouble(tb_1.Text);
b = Convert.ToDouble(tb_2.Text);
sum = a + b;
tb_ans.Text = sum.ToString();
}

private void btn_sub_Click(object sender, EventArgs e)


{
double a, b, sub;
a = Convert.ToDouble(tb_1.Text);
b = Convert.ToDouble(tb_2.Text);
sub = a - b;
tb_ans.Text = sub.ToString();

private void btn_mul_Click(object sender, EventArgs e)


{
double a, b, mul;
a = Convert.ToDouble(tb_1.Text);
b = Convert.ToDouble(tb_2.Text);
mul = a * b;
tb_ans.Text = mul.ToString();
}

private void btn_div_Click(object sender, EventArgs e)


{
double a, b, div;
a = Convert.ToDouble(tb_1.Text);
b = Convert.ToDouble(tb_2.Text);
div = a / b;
tb_ans.Text =Math.Round( div,2).ToString();
}

private void tb_ans_TextChanged(object sender, EventArgs e)


{

private void tb_1_TextChanged(object sender, EventArgs e)


{

}
}
}

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;

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)


{
}

private void btn_add_Click(object sender, EventArgs e)


{
string newItem = tb_add.Text;

list.Add(newItem);

listBox1.DataSource = null;
listBox1.DataSource = list;

tb_add.Clear();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

private void btn_delete_Click(object sender, EventArgs e)


{
if (checkBox1.Checked)
{
var selectedItems = listBox1.SelectedItems.Cast<string>().ToList();
var itemsToRemove = new List<string>();

foreach (var item in selectedItems)


{
itemsToRemove.Add(item);
}

foreach (var itemToRemove in itemsToRemove)


{
list.Remove(itemToRemove);
}

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();
}
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)


{
if (checkBox2.Checked)
{

SortedList<string, int> sortedList = new SortedList<string, int>();


for (int i = 0; i < list.Count; i++)
{
sortedList.Add(list[i], i);
}

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.

You might also like