Lab Manual Dot Net

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

Experiment 1:

1. Accept a character from console and check the case of the character.
using System;
class Program
{
static void Main(string[] args)
{
// Step 1: Accept input character from the console
Console.Write("Enter a character: ");
char inputChar = Console.ReadKey().KeyChar;
Console.WriteLine(); // Move to the next line
// Step 2: Check if the input character is lowercase
if (char.IsLower(inputChar))
{
Console.WriteLine("The entered character is in lowercase.");
}
// Step 3: Check if the input character is uppercase
else if (char.IsUpper(inputChar))
{
Console.WriteLine("The entered character is in uppercase.");
}
// Step 4: If the character is neither lowercase nor uppercase, it's not an alphabet character
else
{
Console.WriteLine("The entered character is not an alphabet character.");
}
}
}
Explanation:
1. **Accept Input:** The program prompts the user to enter a character using `Console.Write()` and reads the character
input using `Console.ReadKey().KeyChar`. It stores the input character in the variable `inputChar`.

2. **Check Lowercase:** It checks if the entered character is in lowercase using `char.IsLower(inputChar)`.


3. **Check Uppercase:** It checks if the entered character is in uppercase using `char.IsUpper(inputChar)`.

4. **Output:** Depending on the check results, it prints whether the character is in lowercase, uppercase, or not an
alphabet character using `Console.WriteLine()`.

Enter a character: A
The entered character is in uppercase.
Experiment 2:
2.Write a program to accept any character from the keyboard and display whether it is a vowel or not.
using System;
class Program
{
static void Main(string[] args)
{
// Step 1: Accept input character from the keyboard
Console.Write("Enter a character: ");
char inputChar = Console.ReadKey().KeyChar;
Console.WriteLine(); // Move to the next line
// Step 2: Convert the input character to lowercase for case-insensitive comparison
char lowerChar = char.ToLower(inputChar);

// Step 3: Check if the input character is a vowel


if (lowerChar == 'a' || lowerChar == 'e' || lowerChar == 'i' || lowerChar == 'o' || lowerChar == 'u')
{
Console.WriteLine("The entered character is a vowel.");
}
else
{
Console.WriteLine("The entered character is not a vowel.");
}
}
}
Explanation:
1. **Accept Input:** Similar to the previous program, the program prompts the user to enter a character using
`Console.Write()` and reads the character input using `Console.ReadKey().KeyChar`. It stores the input character in the
variable `inputChar`.
2. **Convert to Lowercase:** To perform a case-insensitive check, the input character is converted to lowercase using
`char.ToLower(inputChar)` and stored in `lowerChar`.
3. **Check Vowel:** It checks if the lowercase input character is one of the vowels ('a', 'e', 'i', 'o', 'u').
4. **Output:** Depending on the check result, it prints whether the character is a vowel or not using
`Console.WriteLine()`.
Output:Enter a character: E
The entered character is a vowel.
Experiment 3:
3. Write a program to accept a string and convert the case of the characters.
using System;
class Program
{
static void Main(string[] args)
{
// Step 1: Accept input string from the console
Console.Write("Enter a string: ");
string inputString = Console.ReadLine();

// Step 2: Convert the string to uppercase


string upperCaseString = inputString.ToUpper();

// Step 3: Convert the string to lowercase


string lowerCaseString = inputString.ToLower();

// Step 4: Output the original, uppercase, and lowercase strings


Console.WriteLine("Original String: " + inputString);
Console.WriteLine("Uppercase: " + upperCaseString);
Console.WriteLine("Lowercase: " + lowerCaseString);
}
}
Explanation:
1. **Accept Input:** The program prompts the user to enter a string using `Console.Write()` and reads the input string
using `Console.ReadLine()`. It stores the input string in the variable `input String`.
2. **Convert to Uppercase:** It converts the input string to uppercase using `ToUpper()` method and stores it in the
variable `upperCaseString`.
3. **Convert to Lowercase:** It converts the input string to lowercase using `ToLower()` method and stores it in the
variable `lowerCaseString`.
4. **Output:** The program then prints the original, uppercase, and lowercase versions of the input string.
Output:
Enter a string: Hello World
Original String: Hello World
Uppercase: HELLO WORLD
Lowercase: hello world
Experiment 4:
4. Develop a menu based application to implement a text editor with cut, copy, paste, save and close operations.

Step 1: Open Visual Studio and Create a new project( Windows Form Application). Give it a Suitable name(Here
we have named it as “NotePad1”).

Step 2: Click on the Create button. You will come across a Form as given below:

Step 3: Change the name of the form from its properties. This will be displayed on the top of the Notepad as its
heading.
From Toolbox, select menu strip and place it on the top in the form area. In MenuStrip, you can provide the names
of the various options that you want in your notepad. We are adding File, Edit, and Format options in the menu.
You can add more as per your choice.

Step 4: Now in the options provided in the MenuBar, we need to have a dialog box that will open up as the user
clicks on the File, Edit, or Font option. Therefore, we will provide further options for them.

rly, for Edit and Font, we will add the options as shown
below:

Step 5: Now we need to add a RichTextBox control from the toolbox in the form so that the user may enter input in
that field. Here, we are not adding a simple textbox because it can basically take single line input whereas
RichTextBox provides more control over styling the text.ll over and covers the notepad screen area completely.
Anchor Property:

Dock property:

Step 6: Now We need to Provide functionality to the various options in the dialog box. For options like Save,
Open, Font, and Color, we need some special controls from the toolbox.

For save: We need to add

The code for the whole notepad is given below:


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.IO;

// assembly for file handling//


namespace NotePad1

public partial class Form1: Form

public Form1()

InitializeComponent();

// to open a new file //

private void newToolStripMenuItem_Click(object sender, EventArgs e)

richTextBox1.Clear();

// to open a file//

private void openToolStripMenuItem_Click(object sender, EventArgs e)

{ if(openFileDialog1.ShowDialog()== DialogResult.OK)

{ richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);

// to save a file //

private void saveToolStripMenuItem_Click(object sender, EventArgs e)

{ saveFileDialog1.DefaultExt = ".txt";

saveFileDialog1.Filter = "Text File|*.txt|PDF file|*.pdf|Word File|*.doc";

DialogResult dr = saveFileDialog1.ShowDialog();

if (dr == DialogResult.OK)

File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);

// to exit and close notepad//

private void exitToolStripMenuItem_Click(object sender, EventArgs e)


{

Close();

// to cut some data from the textbox //

private void cutToolStripMenuItem_Click(object sender, EventArgs e)

richTextBox1.Cut();

// to copy some data in the textbox //

private void copyToolStripMenuItem_Click(object sender, EventArgs e)

richTextBox1.Copy();

// to paste some copied data//

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)

richTextBox1.Paste();

// to select all the text in the text field //

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)

richTextBox1.SelectAll();

}
Experiment-5
5. Write a program to implement a calculator with memory and recall operations.

Program
using System;
using System.Collections.Generic; using
System.ComponentModel; using System.Data;
using System.Drawing; using
System.Text;
using System.Windows.Forms;
namespace WindowsApplication7
{
public partial class Form1 : Form
{
int n,a,b,r; string op;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
textBox1.Clear();

}
private void button2_Click(object sender, EventArgs e)
{
n = int.Parse(textBox1.Text);
textBox1.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = "" + n;
}
private void button4_Click(object sender, EventArgs e)
{
textBox1.AppendText("1");
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.AppendText("2");
}
private void button6_Click(object sender, EventArgs e)
{
textBox1.AppendText("3");
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.AppendText("4");
}
private void button8_Click(object sender, EventArgs e)
{
textBox1.AppendText("5");
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.AppendText("6");
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.AppendText("7");
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.AppendText("8");
}
private void button12_Click(object sender, EventArgs e)
{
textBox1.AppendText("9");
}
private void button13_Click(object sender, EventArgs e)
{
textBox1.AppendText("10");
}
private void button14_Click(object sender, EventArgs e)
{
a = int.Parse(textBox1.Text); op = "+";
textBox1.Clear();
}
private void button15_Click(object sender, EventArgs e)
{
a = int.Parse(textBox1.Text); op = "-";
textBox1.Clear();
}
private void button16_Click(object sender, EventArgs e)
{
a = int.Parse(textBox1.Text); op = "*";
textBox1.Clear();
}
private void button17_Click(object sender, EventArgs e)
{
a = int.Parse(textBox1.Text); op = "/";
textBox1.Clear();
}
private void button18_Click(object sender, EventArgs e)
{
b = int.Parse(textBox1.Text); switch (op)
{
case "+":
r = a + b; textBox1.Text = "" + r;
break;
case "-":
r = a - b;
textBox1.Text = "" + r; break;
case "*":
r = a * b; textBox1.Text = "" + r;
break;
case "/":
r = a / b; textBox1.Text = "" + r;
break;
}
}
}
}

Output
Experiment- 6
6. Develop a form in to pick a date from Calendar control and display the day, month, year details in separate text
boxes.
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 month_calender_using_textbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)


{

textBox1.Text = dateTimePicker1.Value.Day.ToString();
textBox2.Text = dateTimePicker1.Value.Month.ToString();
textBox3.Text = dateTimePicker1.Value.Year.ToString();

}
Open visual studio 2017 ide and go to create project option then click on visual c# windows application with dot net
framework after that create project using project name
Then go to toolbar option select textbox drag and drop into form
And select calendar option from toolbar and drag n drop into form.
Then double click onto calendar then open coding windows then apply code for calendar control.

Output :
Experiment- 7

7. Develop a application to perform timer based quiz of 10questions.

Design

Program
using System;
using System.Collections.Generic; using
System.ComponentModel; using
System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
string[,] q = new string[10, 4]; string[,]
ans = new string[10, 2]; int i =-1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
q[0, 0] = "WWW stands for";
q[0, 1] = "World Wide Web";
q[0, 2] = "World Wide Web Consortium"; q[0,
3] = "World Web Wide";
q[1, 0] = "XML stands for";
q[1, 1] = "Extended Markup Language"; q[1,
2] = "External Markup Language"; q[1, 3] =
"None";
q[2, 0] = "Pointer is a variable that holds"; q[2, 1] =
"Content";
q[2, 2] = "Data";
q[2, 3] = "Address";
q[3, 0] = "Array is a Collection of"; q[3, 1] =
"Hetrogeneous elements"; q[3, 2] =
"Relational elements";
q[3, 3] = "Homogeneous elements";
q[4, 0] = "CLR executes"; q[4, 1] =
"Managed Code"; q[4, 2] =
"Execution Engine";
q[4, 3] = "Thread Management Unit";
q[5, 0] = "Which one of the following browser ?"; q[5, 1] =
"Visual Basic Managed Code";
q[5, 2] = "Internet Explorer"; q[5, 3]
"C#";

q[6, 0] = "Scanner is a"; q[6, 1] =


"Input Device"; q[6, 2] = "Pointing
Device"; q[6, 3] = "Output
Device";
q[7, 0] = "SQL Server is a"; q[7, 1]
= "Back End Unit"; q[7, 2] = "Front
End";
q[7, 3] = "None";
q[8, 0] = "JIT is a";
q[8, 1] = "Interpreter"; q[8, 2]
= "Assembler";
q[8, 3] = "Compiler";
q[9, 0] = "IL code is a"; q[9, 1] =
"Managed Code"; q[9, 2] =
"Unmanaged Code"; q[9, 3] =
"None";
ans[0, 0] = "World Wide Web";
ans[1, 0] = "Extended Markup Language";
ans[2, 0] = "Address";
ans[3, 0] = "Homogeneous elements";
ans[4, 0] = "Managed Code";
ans[5, 0] = "Internet Explorer"; ans[6,
0] = "Input Device"; ans[7, 0] = "Front
End";
ans[8, 0] = "Compiler"; ans[9, 0] =
"Managed Code"; timer1.Enabled =
true; button1.Enabled = false;
radioButton1.Visible = false;
radioButton2.Visible = false;
radioButton3.Visible = false;

}
private void timer1_Tick(object sender, EventArgs e)
{
i = i + 1; if (i
== 10)
{
radioButton1.Visible = false;
radioButton2.Visible = false;
radioButton3.Visible = false;
timer1.Enabled = false; button1.Enabled =
true; textBox1.Text = "";

}
else
{ radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
textBox1.Text = q[i, 0];
radioButton1.Visible = true;
radioButton2.Visible = true;
radioButton3.Visible = true;

radioButton1.Text = q[i, 1];


radioButton2.Text = q[i, 2];
} radioButton3.Text = q[i, 3];
}
private void radioButton1_Click(object sender, EventArgs e)
{
}
private void radioButton2_Click(object sender, EventArgs e)
{
}
private void radioButton3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int count = 0;
for (int j = 0; j <= 9; j++)
{
if (ans[j,0] == ans[j,1]) count+
+;
}
MessageBox.Show("You have scored " + count + " marks");
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
ans[i, 1] = radioButton1.Text;

}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
ans[i, 1] = radioButton2.Text;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
ans[i, 1] = radioButton3.Text;
}
}
}

Output:
Experiment- 8

8. Develop a database application to store the details of students using ADO.NET

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

namespace student
{
public partial class Form1 : Form
{

SqlConnection con= new SqlConnection("Data Source =.; Initial Catalog = students; Integrated Security = True");
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into student values(@id,@name,@course,@phoneno)",con);

cmd.Parameters.AddWithValue("@id",int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@name",textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);

cmd.Parameters.AddWithValue("@phoneno",textBox4.Text.ToString());
cmd.ExecuteNonQuery();
//it run the sql statement how many rows effected
con.Close ();

MessageBox.Show("successfully saved");
}

}
}

Database Sql server:


Output:

Experiment- 9
9. Develop a database application using ADO.NET to insert, modify, update and delete operations.

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

namespace student
{
public partial class Form1 : Form
{

SqlConnection con= new SqlConnection ("Data Source =.; Initial Catalog = students; Integrated Security = True");
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
con.Open();
cmd = new SqlCommand("insert into student values(@id,@name,@course,@phoneno)",con);

cmd.Parameters.AddWithValue ("@id",int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue ("@name",textBox2.Text);
cmd.Parameters.AddWithValue ("@course", textBox3.Text);

cmd.Parameters.AddWithValue ("@phoneno", textBox4.Text.ToString ());


cmd.ExecuteNonQuery ();
//it run the sql statement how many rows effected
con.Close ();
bindata();
MessageBox.Show("successfully saved");
}
private void button5_Click(object sender, EventArgs e)
{
this.close();
}

private void button2_Click(object sender, EventArgs e)


{
con.Open();
cmd = new SqlCommand("update student set name=@name,course=@course,phoneno=@phoneno where id=@id", con);
cmd.Parameters.AddWithValue("@id", int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);

cmd.Parameters.AddWithValue("@phoneno", textBox4.Text.ToString());
cmd.ExecuteNonQuery();
//it run the sql statement how many rows effected
con.Close();
MessageBox.Show("successfully updated");
}

private void button4_Click(object sender, EventArgs e)


{
con.Open();
cmd = new SqlCommand("delete from student where id=@id", con);
cmd.Parameters.AddWithValue("@id", textBox1.Text);
cmd.ExecuteNonQuery();//it run the sql statement how many rows effected
con.Close();
MessageBox.Show("successfully deleted");
}

Output:

Experiment- 10
10. Develop an application using Data grid to display records.

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

namespace student
{
public partial class Form1 : Form
{

SqlConnection con= new SqlConnection("Data Source =.; Initial Catalog = students; Integrated Security = True");
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
void bindata()
{

cmd = new SqlCommand("select * from student", con);


SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("select * from student", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill (dt);
dataGridView1.DataSource = dt;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);
}
}
}
Experiment- 11
11. Develop a application using Data grid to add, edit and modify records

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

namespace student
{
public partial class Form1 : Form
{

SqlConnection con= new SqlConnection("Data Source =.; Initial Catalog = students; Integrated Security = True");
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
void bindata()
{

cmd = new SqlCommand("select * from student", con);


SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("insert into student values(@id,@name,@course,@phoneno)",con);
cmd.Parameters.AddWithValue("@id",int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@name",textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);

cmd.Parameters.AddWithValue("@phoneno",textBox4.Text.ToString());
cmd.ExecuteNonQuery();
//it run the sql statement how many rows effected
con.Close ();
bindata();
MessageBox.Show("successfully saved");
}

private void button5_Click(object sender, EventArgs e)


{
this.Close();
}

private void button2_Click(object sender, EventArgs e)


{
con.Open();
cmd = new SqlCommand("update student set name=@name,course=@course,phoneno=@phoneno where id=@id", con);
cmd.Parameters.AddWithValue("@id", int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@course", textBox3.Text);

cmd.Parameters.AddWithValue("@phoneno", textBox4.Text.ToString());
cmd.ExecuteNonQuery();
//it run the sql statement how many rows effected
con.Close();
MessageBox.Show("successfully updated");
}

private void button3_Click(object sender, EventArgs e)


{
cmd = new SqlCommand("select * from student", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill (dt);
dataGridView1.DataSource = dt;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 12, FontStyle.Bold);

private void button4_Click(object sender, EventArgs e)


{
con.Open();
cmd = new SqlCommand("delete from student where id=@id", con);
cmd.Parameters.AddWithValue("@id", textBox1.Text);
cmd.ExecuteNonQuery();//it run the sql statement how many rows effected
con.Close();
MessageBox.Show("successfully deleted");
}

}
}

Experiment- 12
12. Develop a Window application to read an XML document containing subject, mark scored, and year of
passing into a Dataset.
1. Open visual Studio 2005
2. Click File -> New -> Project -> Visual C# -> Window application
3. On the Right click of solution explorer, Click Add -> New Item -> XML File
4.Type the following XM+L Document
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<name> aswin</naame>
<sub>cbt</sub>
<mark>80</mark>
<yop>2018</yop>
</student>
<student>
<name> arun</naame>
<sub>web</sub>
<mark>90</mark>
<yop>2018</yop>
</student>
</students>

5. Click Save Button


6. On the Form1 Click Event , type the Following Code
[Note : To Select the XML file path, Click XML
File and select path from property window]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace studentxmlintodataset
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string path = @"C:\Users\sadha\Documents\Visual Studio 2005\Projects\
studentxmlintodataset\studentxmlintodataset\XMLFile1.xml";
dataSet1.ReadXml(path);
dataGridView1.DataSource = dataSet1;
dataGridView1.DataMember = "student";
}
}
}
6. On the Form1, ADD Dataset Control and select untyped dataset option from the Add Dataset
Dialogue box

7. Run the Application

Design

DataGridView Control

Output:

You might also like