Nothing Special   »   [go: up one dir, main page]

CBT N Scheme Lab Manual-Part - B

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

PART - B

Ex No: 01 C#.NET PROGRAM FOR TEXT EDITOR

AIM
To develop a menu based application to implement a text editor with cut,
copy, paste, save and close operations using C#.

PROCEDURE:
1. Open Microsoft Visual Studio File-> New -> Project.
2. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.

SOURCE CODE:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.linq;
Using System.Text;
Using System.Windows.Forms;
namespace WindowsFormsApplication1
{
Public partial class Form1:Form
{
Public Form1()
{
InitializeComponent();
}
Private void Form1_Load(object sender, EventArgs e)
{
Page | 1
Label1.Visible=false;
textBox1.Visible=false;
Button1.Visible=false;
}
Private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
Private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Label1.Visible=true;
textBox1.Visible=true;
Button1.Visible=true;
}
Private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
Private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cur();
}
Private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
Private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
Private void buton1_Click(object sender, EventArgs e)
{
richTextBox1.SaveFile(textBox1.Text);

Page | 2
}

OUTPUT:

RESULT:
Thus the C# program to implement a text editor is created and executed
successfully.
Page | 3
Ex No: 02 C#.NET PROGRAM FOR TIMER BASED QUIZ APPLICATION

AIM:
To develop a c# application to perform timer based quiz of 5 questions using
C#.

PROCEDURE
1. Open Microsoft Visual Studio File-> New -> Project.
2. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.

SOURCE CODE:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.linq;
Using System.Text;
Using System.Windows.Forms;
namespace WindowsFormsApplication1
{
Public partial class Form1:Form
{
String[,]Quest=new string[5,4];
String[,]Ans=new string[5,2];
int i= -1;
Public Form1()
{

Page | 4
InitializeComponent();
}
Private void Form1_Load(object sender, EventArgs e)
{
Quest[0,0]=”in……………..india won cricket world cup”;
Quest[0,1]=”1973”;
Quest[0,2]=”1983”;
Quest[0,3]=”1987”
Quest[1,0]=”scanner is ………….. device”;
Quest[1,1]=”input”;
Quest[1,2]=”ouput”;
Quest[1,3]=”I/O”;
Quest[2,0]=”which one of the following is a browser?”;
Quest[2,1]=”visual basic”;
Quest[2,2]=”java”;
Quest[2,3]=”chrome”;
Quest[3,0]=”………… was not a chief minister”;
Quest[3,1]=”kamarajar”;
Quest[3,2]=”sivaji”;
Quest[3,3]=”MGR”;
Quest[4,0]=”tsunami hits india on …………”;
Quest[4,1]=”DEC 24,2006”
Quest[4,2]=”DEC 24,2004”;
Quest[4,3]=”DEC 26,2006”;
Ans[0,0]=”1983”;
Ans[0,0]=”input”;
Ans[0,0]=”chrome”;
Ans[0,0]=”sivaji”;
Ans[0,0]=”DEC 4,2004”;
Timer1.Enabled=true;
Button1.Enabled=false;
}
Private void timer1_Tick(object sender, EventArgs e)

Page | 5
{
i=i + 1;
If(i==5)
{
Timer1.Enabled=false;
Button1.Enabled=true;
}
Else
{
radioButton1.Checked=false;
radioButton2.Checked=false;
radioButton3.Checked=false;
textBox1.Text=Quest[i,0];
radioButton1.Text=Quest[i,1];
radioButton2.Text=Quest[i,2];
radioButton3.Text=Quest[i,3];
}
}
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;
}
Private void button1_Clickobject sender, EventArgs e)
{
Int count=0;

Page | 6
For(int j=0; j<=4; j++)
{
If(Ans[j,0]==Ans[j,1])
{
Count+=1;
}
}
MessageBox.Show(“you have scored”+count+”marks”);
}
}
}

OUTPUT:

RESULT:

Page | 7
Thus a C# program for timer based quiz application has created and executed
successfully.

Ex No: 3 ADO.NET PROGRAM FOR INSERTION DELETION AND


UPDATION

AIM:
To write an ADO.NET program for insertion, deletion and updating in student
database

PROCEDURE:
1. Open Microsoft Visual Studio -> Create a table and database in Server Explorer
window.
2. In the table create appropriate column name and data type and insert the data in
table.
3. Open Microsoft Visual Studio File-> New -> Project.
4. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.
5. Design controls on a form1, write event procedures and run the application.
SOURCE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

Page | 8
namespace WindowsApplication15
{
public partial class Form1 : Form
{
public SqlConnection con;
public SqlCommand com;
public SqlDataReader rd;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection("data source=pa-2259bf176d4d;initial
catalog=students; persist security info=True; Integrated Security=SSPI;");
//while using sql query analyzer/ Con= new
SqlConnection(“Server=localhost;database=student;uid=sa;pwd=”);
com=new SqlCommand();
com.Connection=con;
con.Open();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
comboBox1.Visible = false;
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)

Page | 9
{
comboBox1.Items.Clear();
button1.Enabled=false;
button2.Enabled=true;
button3.Enabled=true;
comboBox1.Visible=true;
com.CommandText="select*from stu";
rd=com.ExecuteReader();
while(rd.Read())
{
comboBox1.Items.Add(rd.GetString(0));
}
rd.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
com.CommandText="select * from stu where
regno=('"+comboBox1.SelectedItem+"')";
rd=com.ExecuteReader();
if(rd.Read())
{
textBox2.Text=rd.GetString(1);
textBox3.Text=rd.GetString(2);
}
rd.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text==""||textBox2.Text==""||textBox3.Text=="")
{
MessageBox.Show("Please fill all details");
}
else

Page | 10
{
com.CommandText="insert into stu
values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
com.ExecuteNonQuery();
MessageBox.Show("record inserted");
}
}
private void button2_Click(object sender, EventArgs e)
{
com.CommandText="delete from stu where
regno="+comboBox1.SelectedItem+"";
rd=com.ExecuteReader();
comboBox1.Items.Remove(comboBox1.SelectedItem);
textBox2.Clear();
textBox3.Clear();
MessageBox.Show("record deleted");
}
private void button3_Click(object sender, EventArgs e)
{
com.CommandText="update stu set
name='"+textBox2.Text+"',dept='"+textBox3.Text+"' where
regno='"+comboBox1.SelectedItem+"'";
com.ExecuteNonQuery();
MessageBox.Show("record updated");
}
}
}

OUTPUT:

Page | 11
RESULT:
Thus an ADO.NET program for insertion, deletion and Updating is created
and executed successfully.

Page | 12
Ex No: 4 DATA GRID APPLICATION WITH INSERTION AND
UPDATION

AIM:
To create a application using datagrid to add, edit, and modify records.

PROCEDURE:
1. Create a table stud in a sql server database student ( using SQL Query Analyzer).
2. Execute the following commands
Create database student
Use student
Create table stud (regno varchar(10), name varchar(10), dept varchar(10))
3. Open Microsoft Visual Studio File-> New -> Project.
4. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.

SOURCE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ex12
{
public partial class Form1 : Form
{

Page | 13
SqlCommand com = new SqlCommand();
SqlConnection con = new SqlConnection("Data Source=LAB2-36;Initial
Catalog=student;Integrated Security=True");
SqlDataReader rd;
int n;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
com.Connection = con;
con.Open();
}
private void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
com.CommandText="update studs set
sname='"+dataGridView1.CurrentRow.Cells[1].Value+"',address='"+dataGridView1.
CurrentRow.Cells[2].Value+"',dept='"+dataGridView1.CurrentRow.Cells[3].Value+"'
where regno='"+dataGridView1.CurrentRow.Cells[0].Value+"'";
com.ExecuteNonQuery();
MessageBox.Show("One record updated....");
SqlDataAdapter da1=new SqlDataAdapter("select * from studs",con);
DataSet ds1=new DataSet();
da1.Fill(ds1,"studs");
dataGridView1.DataSource=ds1.Tables["studs"];
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Visible=false;
com.CommandText="select count(*) from studs";
n=(Int32)com.ExecuteScalar();

Page | 14
textBox1.Text="S000"+n+1;
com.CommandText="insert into studs
values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Tex
t+"')";
com.ExecuteNonQuery();
MessageBox.Show("One record inserted....");
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Visible=true;
SqlDataAdapter da=new SqlDataAdapter("select * from studs",con);
DataSet ds=new DataSet();
da.Fill(ds,"studs");
dataGridView1.DataSource=ds.Tables["studs"];
}
}
}

OUTPUT:

Page | 15
RESULT:
Thus the ADO.NET with data grid has been implemented for insertion and
Updating operation.

Page | 16
Ex:No 5 REQUIRED FIELD VALIDATOR AND RANGEVALIDATOR
CONTROLS

AIM:
To Develop a web application to input data through a web form to a database
and validate the data. Use the Required Field Validator and RangeValidator Controls.

PROCEDURE:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"


Inherits="WebApplication5._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
<title>LOGIN PAGE</title>
<style type="text/css">
#form1
{
height: 519px;
}
</style>
</head>
<body bgcolor="#cccccc">
<form id="form1" runat="server">
<div>

</div>
<asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="166px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Enter
Username">*</asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="166px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Enter
Password">*</asp:RequiredFieldValidator>

Page | 17
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Re-Enter Password"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Width="166px"></asp:TextBox>
<asp:CompareValidator ID="Password" runat="server"
ControlToCompare="TextBox2" ControlToValidate="TextBox3"
ErrorMessage="Re-Enter Password"
CultureInvariantValues="True">*</asp:CompareValidator>
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Age"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Width="166px"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Not Eligible"
MaximumValue="58"
MinimumValue="18">*</asp:RangeValidator>
<br />
<br />
<asp:Label ID="Label5" runat="server" Text="E-mail"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Width="166px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="TextBox5" ErrorMessage="Enter valid E-mail ID"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</
asp:RegularExpressionValidator>
<br />
<br />
<asp:Label ID="Label6" runat="server" Text="PhoneNumber"></asp:Label>
<asp:TextBox ID="TextBox6" runat="server" Width="166px"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox6" ErrorMessage="Enter 10 digit
PhoneNmuber">*</asp:CustomValidator>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" >Text="Submit" />
<br />
<br />
</form>
</body>
</html>

Page | 18
Output:

Page | 19
Ex No: 6 READ STUDENT DATA FROM XML DOCUMENTAND DISPLAY

AIM:
To develop a application to read an xml document containing subject, marks
scored, year of passing into a dataset.

PROCEDURE:
1. Open Microsoft Visual Studio File-> New -> Project.

Page | 20
2. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.
3. On Project menu click Add New Item -> then click XML file option -> enter the
XML filename as student.xml.
4. On Form1 add button 1 and DataGridView and set name as show student details
for button 1. 5. On Form1 add a dataset control and select untyped dataset option and
click ok.
6. Enter the code for button1.
7. Run the application.

SOURCE CODE:
student.xml
<?xml version="1.0" encoding="utf-8" ?>
<student>
<stud>
<name> babu </name>
<subject>cse</subject>
<marks>567</marks>
<year>2012</year>
</stud>
<stud>
<name> mohan</name>
<subject>mechanical</subject>
<marks>587</marks>
<year>2011</year>
</stud>
<stud>
<name> soundhar </name>
<subject>eee</subject>
<marks>577</marks>
<year>2013</year>
</stud>
</student>

Page | 21
Click event of buton1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace exb2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataSet dataset = new DataSet();
dataset.ReadXml(@"C:\Documents and Settings\cse\My Documents\Visual
Studio 2005\Projects\exb2\exb2\students.xml");
dataGridView1.DataSource = dataset.Tables[0];

}
}
}

OUTPUT:

Page | 22
RESULT:
thus the above xml document was read by datagridview and executed
successfully.

Ex No:7 GENERATE XML DOCUMENT CONTAINING


STUDENTS RECORDS

AIM:
To develop a Window application to read students records from Database
using ADO.NET and generate XML document containing students records.

Page | 23
PROCEDURE:
1. Open Microsoft Visual Studio -> Create a table and database in Server Explorer
window.
2. In the table create appropriate column name and data type and insert the data in
table.
3. Open Microsoft Visual Studio File-> New -> Project.
4. Select project type as Visual C# -> Windows and Template type as Windows
application, then click OK.
5. Drag and drop the dataset and datagridview control on form1.
6. Write event procedure and run application.

SOURCE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication12
{
public partial class Form1 : Form
{
public SqlConnection con;
public SqlCommand com;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)

Page | 24
{
SqlConnection conn = new SqlConnection("Data Source=pa-
2259bf176d4d;initial Catalog=students;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from stu", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ds.Tables[0].WriteXml(@"D:\student.xml");
dataGridView1.DataSource = ds.Tables[0];
MessageBox.Show("XML DOCUMENT IS GENERATED AT LOCATION
D DRIVE AS student.XML");

}
}
}
OUTPUT:

Page | 25
RESULT:
Program code to read students records from Database using ADO.NET and
generate XML document containing students records is verified.

Page | 26