Best Lab Manual of C# Programming
Best Lab Manual of C# Programming
Best Lab Manual of C# Programming
MANUAL:
CSE 300
SOFTWARE
DEVELOPMENT
USING C# and .NET
MD MAMUNUR RASHID
AKAND & NABIL BIN HANNAN
Lecturer, Dept of CSE, AUST.
1
CONTENTS
LAB 01: INTRODUCTION TO .NET AND VISUAL C# ........................................................................................ 4
Downloading visual studio ........................................................................................................................ 4
Different versions of visual studio (paid and free ones) ........................................................................... 4
Creating a simple console application which displays hello world ........................................................... 4
Taking non-numeric data from keyboard into console application.......................................................... 4
Taking numeric data in console application ............................................................................................. 5
Handling errors using try and catch block ................................................................................................ 5
LAB 02: C# CONSOLE APPLICATION .............................................................................................................. 6
Using "IF" and "ELSE" to define conditions in C# applications ................................................................. 6
Putting comments in c# code ................................................................................................................... 6
Using FOR loop .......................................................................................................................................... 7
Creating a simple console calculator ........................................................................................................ 7
LAB 03: C# WINDOWS APPLICATION ............................................................................................................ 8
Creating windows application................................................................................................................... 8
Creating a simple customer screen which takes Customer name, Country, Gender, Hobby and Status
................................................................................................................................................................ 10
Creating a preview screen that will display data entered in to the customer data entry screen .......... 11
LAB 04: NAVIGATIONAL MENUS, OOP AND CONNECTING TO DATABASE ................................................. 13
Creating navigational menus which will help us to navigate Customer entry screen and display screens
easily ....................................................................................................................................................... 13
Reusing code by creating classes and objects and form validation ........................................................ 14
Connecting to SQL Server, getting data and getting acquainted with ADO.NET components, like
connection, command and data reader ................................................................................................. 16
LAB 05: INSERTING INTO DATABASE AND DYNAMIC CONNECTION STRING.............................................. 18
Inserting data from the UI to SQL Server ................................................................................................ 18
Making the connection string configurable by storing it in a XML configuration files rather than hard-
coding it in the C# code........................................................................................................................... 19
LAB 06: UPDATING AND DELETING FROM DATABASE ................................................................................ 19
Selecting data when a user clicks on the data grid hyper link ................................................................ 19
updating data by using the UPDATE SQL command using the ADO.NET command object ................... 20
Implementing delete functionality by writing the delete SQL command in the ADO.NET command
object ...................................................................................................................................................... 21
2
LAB 07: TWO-LAYERED ARCHITECTURE ...................................................................................................... 21
Breaking the project into a 2-layered architecture, the first layer will have UI + business logic and the
second layer will have the SQL part i.e. data access............................................................................... 21
LAB 08: CREATING ASP.NET WEB APPLICATION ......................................................................................... 21
Creating an ASP.NET web application UI that will interact with the customer data entry database..... 21
Connecting the ASP.NET Web application UI with the data access layer ............................................... 21
Implementing insert, update and delete functionality for the ASP.NET web application UI. ................ 21
LAB 09: THREE-LAYERED ARCHITECTURE.................................................................................................... 22
Converting the 2-layered architecture to 3-layered architecture (3 layers: UI, business logic and data
access) ..................................................................................................................................................... 22
LAB 10: SECURITY ........................................................................................................................................ 23
Implementing security in Windows applications .................................................................................... 23
Implementing Web security using Forms Authentication ...................................................................... 23
LAB 11: VALIDATION AND CRYSTAL REPORT .............................................................................................. 23
Implementing email validation easily using a regular expression (REGEX) ............................................ 23
Creating Crystal Reports ......................................................................................................................... 23
LAB 12: DATABASE NORMALIZATION AND RELATIONSHIPS....................................................................... 23
Applying normalization rules to make our database more structured so that we can avoid redundant
and duplicate data .................................................................................................................................. 23
Understanding 1 to many relationship between customer and hobbies............................................ 23
3
LAB 01: INTRODUCTION TO .NET AND VISUAL C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Output:
Hello World!
4
namespace Lab01
{
class Program
{
static void Main(string[] args)
{
string Name = "";
Console.Write("Please Enter your name: ");
Name = Console.ReadLine();
Console.WriteLine ("User Name: " + Name);
}
}
}
namespace Lab01
{
class Program
{
static void Main(string[] args)
5
{
int Age = 0;
Console.Write("Please Enter your age: ");
try
{
Age = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("User Name: " + Age);
}
catch (Exception)
{
Console.WriteLine("You must Enter Numeric value as your age.");
}
}
}
}
Sample Output:
Please Enter your age: Mamun
You must Enter Numeric value as your age.
namespace Lab02
{
class Program
{
static void Main(string[] args)
{
string Name = "";
Console.Write("Please enter your name: ");
Name = Console.ReadLine();
if (Name.Length > 10)
Console.WriteLine("Name must be within 10 characters.");
else
Console.WriteLine("User Name: " + Name);
}
}
}
6
class Program
{
static void Main(string[] args)
{
string Name = "";
Console.Write("Please enter your name: "); // You can put a single line comment
/* or, if you wish,
* you can put a multiple line comment
* like this
* */
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
Console.Write(i);
}
}
Sample Output:
0123456789
7
Console.Write("Enter operator: ");
string op = Console.ReadLine();
double result = 0;
bool valid = true;
switch (op)
{
case "+":
result = n1 + n2;
break;
case "-":
result = n1 - n2;
break;
case "*":
result = n1 * n2;
break;
case "/":
result = n1 / n2;
break;
default:
valid = false;
break;
}
if (valid) Console.WriteLine("Result is: " + result);
else Console.WriteLine("Invalid Operator!");
}
}
}
8
the TextBox Right Click Properties In Properties find
Name field and give a unique name to the Textbox. Suppose the
1st TextBox name is txtNumber1 and the 2nd TextBox name is
txtNumber2.
Then we have to go to View ToolBox and add 4 Buttons for the
operations (+,-,*, /). Similarly give each Button a unique name
in the Name field. Here we have given the Buttons name as
follows: btnPlus, btnMinus, btnMultiplication and btnDivision. We
also have to show the symbols (+,-,*, /) on the Button. To do
that, Select the 1st Button Right Click Properties Find
Text field and write +. Do this for the other buttons as well.
We want to click the Buttons and perform the operations
accordingly. So we have to write some code for each Buttons
ClickEvent. To do that, double click each button in the form and
then write the corresponding operations for the 2 numbers given
input in the Textboxes.
Here we are showing the output using a MessageBox.
using System.Windows.Forms;
namespace SimpleForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
9
private void btnDivision_Click(object sender, EventArgs e)
{
double n1 = Convert.ToDouble(txtNumber1.Text);
double n2 = Convert.ToDouble(txtNumber2.Text);
double result = n1 / n2;
MessageBox.Show(n1 + "/" + n2 + " = " + result);
}
}
}
10
CREATING A PREVIEW SCREEN THAT WILL DISP LAY DATA ENTERED IN TO THE CUSTOMER
DATA ENTRY SCREEN
In the Customer Data Entry form, double click the button Preview
and write down the functionality of the clicking event that is
show a form which will contain 5 labels for the titles and
another 5 Labels to show the data that was given as input.
Write a user-defined function SetValues (. . .) that sets the
value of the given input to the Labels.
namespace CustomerDataEntry
{
public partial class frmCustomerDataEntry : Form
{
public frmCustomerDataEntry()
{
InitializeComponent();
}
11
else Status = "Unmarried";
objPreview.Show();
}
}
}
namespace CustomerDataEntry
{
public partial class frmCustomerPreview : Form
{
public frmCustomerPreview()
{
InitializeComponent();
}
12
LAB 04: NAVIGATIONAL MENUS, OOP AND CONNECTING TO DATABASE
In program.cs:
13
REUSING CODE BY CREATING CLASSES AND OBJECTS AND FORM VALIDATION
An important feature of programming is to avoid the reusability
of code.
To write the validations of different types, create a new class.
View Solution Explorer Select your project Right Click
Add New Item Add a new Class and name it as
CustomerValidation.cs
In CustomerValidation.cs you can define various functions with
different names and parameters. An example is given below -
In project Validations:
namespace Validations
{
public class CustomerValidation
{
public void CheckCustomerName(string CustomerName)
{
if (CustomerName.Length > 10)
throw new Exception("Name should be within 10 characters.");
14
else if (CustomerName == "")
throw new Exception("Name is required.");
}
}
}
}
In frmCustomerDataEntry:
using Validations;
namespace Lab04
{
public partial class frmCustomerDataEntry : Form
{
public frmCustomerDataEntry()
{
InitializeComponent();
}
try
{
CustomerValidation objVal = new CustomerValidation();
objVal.CheckCustomerName(txtName.Text);
objPreview.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
15
}
}
CONNECTING TO SQL SERVER, GETTING DATA AND GETTING ACQUAINTED WITH ADO.NET
COMPONENTS, LIKE CONNECTION, COMMAND AND DATA READER
16
2. View ToolBox Data Data GridView Drag it to the
frmCustomerDataEntry form and name it as dtgCustomer.
3. View Server Explorer Here your connection is shown
under Data Connection. Select that new connection Right
Click Properties Connection String (This string is
needed in the code below)
6. Now that your Table has been created, you can add different
columns: Select your Table Right Click Show Table
Definition Now add column names with respective datatypes
7. You can add data in your Table: Select your Table Right
Click Show Table Data
// Fire a Command
string strCommand = "Select * From CustomerTable";
SqlCommand objCommand = new SqlCommand(strCommand, objConnection);
17
LAB 05: INSERTING INTO DATABASE AND DYNAMIC CONNECTION STRING
// Open a Connection
string strConnection = "Data Source=.\\sqlexpress;Initial Catalog=CustomerDB;"
+ "Integrated Security=True";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
// Fire a Command
string strCommand = "insert into CustomerTable values('"+txtName.Text+"', '"
+cmbCountry.Text+"','"
+Gender+"', '"
+Hobby+"', "
+Status+" )";
SqlCommand objCommand = new SqlCommand(strCommand, objConnection);
18
objCommand.ExecuteNonQuery();
loadCustomer();
}
SELECTING DATA WHEN A USER CLICKS ON THE DATA GRID HYPER LINK
19
}
private void clearForm()
{
txtName.Text = "";
cmbCountry.Text = "";
radioMale.Checked = false;
radioFemale.Checked = false;
chkPainting.Checked = false;
chkReading.Checked = false;
radioMarried.Checked = false;
radioUnmarried.Checked = false;
}
UPDATING DATA BY USING THE UPDATE SQL COMMAND USING THE ADO.NET COMMAND
OBJECT
// Fire a Command
string strCommand = "Select * From CustomerTable where CustomerName =
'"+strCustomer+"'";
SqlCommand objCommand = new SqlCommand(strCommand, objConnection);
objConnection.Close();
txtName.Text = objDataSet.Tables[0].Rows[0][0].ToString();
cmbCountry.Text = objDataSet.Tables[0].Rows[0][1].ToString();
string Gender = objDataSet.Tables[0].Rows[0][2].ToString();
if (Gender.Equals("Male")) radioMale.Checked = true;
else radioFemale.Checked = true;
string Hobby = objDataSet.Tables[0].Rows[0][3].ToString();
if (Hobby.Equals("Reading")) chkReading.Checked = true;
else chkPainting.Checked = true;
string Married = objDataSet.Tables[0].Rows[0][4].ToString();
if (Married.Equals("True")) radioMarried.Checked = true;
else radioUnmarried.Checked = true;
}
20
IMPLEMENTING DELETE FUNCTIONALITY BY WRITING THE DELETE SQL COMMAND IN THE
ADO.NET COMMAND OBJECT
// Fire a Command
string strCommand = "Delete from CustomerTable where CustomerName =
'"+txtName.Text+"'";
SqlCommand objCommand = new SqlCommand(strCommand, objConnection);
objCommand.ExecuteNonQuery();
BREAKING THE PROJECT INTO A 2-LAYERED ARCHITECTURE, THE FIRST LAYER WILL HAVE UI
+ BUSINESS LOGIC AND THE SECOND LAYER WIL L HAVE THE SQL PART I.E. DATA ACCESS.
CREATING AN ASP.NET WEB APPLICATION UI T HAT WILL INTERACT WITH THE CUSTOMER
DATA ENTRY DATABASE
CONNECTING THE ASP.NET WEB APPLICATION UI WITH THE DATA ACCESS LAYER
IMPLEMENTING INSERT, UPDATE AND DELETE FUNCTIONALITY FOR THE ASP.NET WEB
APPLICATION UI.
21
LAB 09: THREE-LAYERED ARCHITECTURE
22
LAB 10: SECURITY
23