0% found this document useful (0 votes)
18 views14 pages

Final

The document outlines several practical exercises involving the creation of applications and web forms for user input, data processing, and database interactions. Key tasks include creating a user interface for displaying concatenated names and messages, calculating nutritional calories, user registration with validation, and managing employee records in a database. Each practical includes code snippets in C# and SQL, demonstrating the implementation of the described functionalities.

Uploaded by

Keshav Lekhwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

Final

The document outlines several practical exercises involving the creation of applications and web forms for user input, data processing, and database interactions. Key tasks include creating a user interface for displaying concatenated names and messages, calculating nutritional calories, user registration with validation, and managing employee records in a database. Each practical includes code snippets in C# and SQL, demonstrating the implementation of the described functionalities.

Uploaded by

Keshav Lekhwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Practical No.

8 :- Create an application which will ask the user to input his name and a
message, display the two items concatenated in a label, and change the format of the label
using radio buttons and check boxes for selection, the user can make the label text bold,
underlined or italic and change its color. include buttons to display the message in the label,
clear the text boxes and label and exit.
CODE:
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 practical8_NameAndMessage_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
label3.Text = txtName.Text + " " + txtMessage.Text;
}
private void rdoRed_CheckedChanged(object sender, EventArgs e)
{
if(rdoRed.Checked==true)
label3.ForeColor = Color.Red;
}
private void rdoGreen_CheckedChanged(object sender, EventArgs e)
{
if(rdoGreen.Checked==true)
label3.ForeColor = Color.Green;
}
private void rdoBlue_CheckedChanged(object sender, EventArgs e)
{
if(rdoBlue.Checked == true )
label3.ForeColor = Color.Blue;
}
private void chkBold_CheckedChanged(object sender, EventArgs e)
{
if(chkBold.Checked ==true )
label3.Font = new Font(label3.Font, FontStyle.Bold);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void chkItalic_CheckedChanged(object sender, EventArgs e)
{
if (chkItalic.Checked == true)
{
label3.Font = new Font(label3.Font, FontStyle.Italic);
}
}
private void chkUnderline_CheckedChanged(object sender, EventArgs e)
{
if(chkUnderline.Checked ==true)
label3.Font = new Font(label3.Font, FontStyle.Underline);
}
private void btnClear_Click(object sender, EventArgs e)
{
txtMessage.Clear();
txtName.Clear();
label3.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

OUTPUT:-
Practical No. 9 :- Create a project that calculates the total of fat, carbohydrate and protein.
Allow the user to enter into text boxes. The grams of fat, grams of carbohydrate and grams
of protein. Each gram of fat is 9 calories and protein or carbohydrate is 4 calories. Display
the total calories of the current food item in a label. Use to other labels to display and
accumulated some of calories and the count of items entered. The form food have 3 text
boxes for the user to enter the grams for each category include label next to each text box
indicating what the user is enter.

CODE:
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 calories
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txtFat_TextChanged(object sender, EventArgs e)
{
int n = int.Parse(txtFat.Text);
int m;
m = n * 9;
lblFat.Text = m.ToString();
int t = Convert.ToInt32(lblFat.Text) + Convert.ToInt32(lblCarbohidrate.Text) +
Convert.ToInt32(lblProtine.Text);
label5.Text = t.ToString();
}
private void txtCarbohidrate_TextChanged(object sender, EventArgs e)
{
int n = int.Parse(txtCarbohidrate.Text );
int m;
m = n * 4;
lblCarbohidrate.Text = m.ToString();
int t = Convert.ToInt32(lblFat.Text) + Convert.ToInt32(lblCarbohidrate.Text) +
Convert.ToInt32(lblProtine.Text);
label5.Text = t.ToString();
}
private void txtProtine_TextChanged(object sender, EventArgs e)
{
int n = int.Parse(txtProtine.Text);
int m;
m = n * 9;
lblProtine.Text = m.ToString();
int t = Convert.ToInt32(lblFat.Text) + Convert.ToInt32(lblCarbohidrate.Text) +
Convert.ToInt32(lblProtine.Text);
label5.Text = t.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
lblFat.Text = Convert.ToString(0);
lblCarbohidrate.Text = Convert.ToString(0);
lblProtine.Text = Convert.ToString(0);
}
}
}

OUTPUT:
Practical No. 10 :- Create the web application that accepts name, password, age, email id,
and user id. All the information entry is compulsory. Password should be reconfirmed. Age
should be within 21 to 30. Email id should be valid. User id should have at least a capital
letter and digit as well as length should be between 7 and 20 characters.

CODE:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="YourNamespace._Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>User Registration</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>User Registration</h2>
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" EnableViewState="false"></asp:Label>
<br />
<asp:Label ID="lblName" runat="server" Text="Name:" AssociatedControlID="txtName"></asp:Label>
<asp:TextBox ID="txtName" runat="server" MaxLength="50" Required="true"></asp:TextBox>
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password:"
AssociatedControlID="txtPassword"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Required="true"></asp:TextBox>
<br />
<asp:Label ID="lblConfirmPassword" runat="server" Text="Confirm Password:"
AssociatedControlID="txtConfirmPassword"></asp:Label>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"
Required="true"></asp:TextBox>
<br />
<asp:Label ID="lblAge" runat="server" Text="Age:" AssociatedControlID="txtAge"></asp:Label>
<asp:TextBox ID="txtAge" runat="server" Required="true" ValidationGroup="Registration"></asp:TextBox>
<asp:RangeValidator ID="rangeAge" runat="server" ControlToValidate="txtAge" Type="Integer"
MinimumValue="21" MaximumValue="30"
ErrorMessage="Age should be between 21 and 30." ValidationGroup="Registration"></asp:RangeValidator>
<br />
<asp:Label ID="lblEmail" runat="server" Text="Email:" AssociatedControlID="txtEmail"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" Required="true"
ValidationGroup="Registration"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexEmail" runat="server" ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Invalid email format." ValidationGroup="Registration"></asp:RegularExpressionValidator>
<br />
<asp:Label ID="lblUserId" runat="server" Text="User ID:" AssociatedControlID="txtUserId"></asp:Label>
<asp:TextBox ID="txtUserId" runat="server" Required="true"
ValidationGroup="Registration"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexUserId" runat="server" ControlToValidate="txtUserId"
ValidationExpression="^(?=.*[A-Z])(?=.*\d).{7,20}$"
ErrorMessage="User ID should have at least a capital letter and a digit, and length should be between 7 and 20
characters." ValidationGroup="Registration"></asp:RegularExpressionValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
ValidationGroup="Registration" />
</div>
</form>
</body>
</html>
```
**Code-Behind (Default.aspx.cs):**
```csharp
using System;
using System.Web.UI;
namespace YourNamespace
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// Process the registration
lblMessage.Text = "Registration successful!";
ClearFields();
}
else
{
lblMessage.Text = "Please fix the errors.";
}
}
private void ClearFields()
{
txtName.Text = "";
txtPassword.Text = "";
txtConfirmPassword.Text = "";
txtAge.Text = "";
txtEmail.Text = "";
txtUserId.Text = "";
}
}
}
OUTPUT:
Practical No. 11 : Create a Web App to display all the Empname and Deptid of the employee
from the database using SQL source control and bind it to GridView. Database fields
are(Deptld, DeptName, EmpName, Salary).

CODE:

MySql query:

CREATE TABLE Employees (


DeptId INT,
DeptName NVARCHAR(100),
EmpName NVARCHAR(100),
Salary DECIMAL(18, 2)
);

INSERT INTO Employees (DeptId, DeptName, EmpName, Salary) VALUES (1, 'account', 'yash ', 20000);
INSERT INTO Employees (DeptId, DeptName, EmpName, Salary) VALUES (2, 'Sales', 'rohit', 25000);

Web.config :-

<configuration>
<connectionStrings>
<add name="MyConnectionString"
connectionString="Data Source=YOUR_SERVER_NAME;Initial
Catalog=YOUR_DATABASE_NAME;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

Default.aspx:-

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


Inherits="YourNamespace.Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Employee List</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
GridLines="Both">
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" />
<asp:BoundField DataField="DeptId" HeaderText="Department ID" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Default.aspx.cs:-

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace YourNamespace
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()


{
string connStr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connStr))


{
string query = "SELECT EmpName, DeptId FROM Employees";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}

OUTPUT:-
Practical No. 12 : Create a web application Login Module which adds Usemame and
Password in the database. Usemame in the database should be a primary key.

Solution:
Step 1: Create a database file with following fields: UserName, Password, UserName with primary key attributes.
Step 2: Design the web page as:
Step3: Do the following Code:

CODE:

Default.aspx:-
<asp:Label ID="Label1" runat="server" Text="Username:"></asp:Label>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />

<asp:Label ID="Label2" runat="server" Text="Password:"></asp:Label>


<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><br />

<asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" />


<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

Web.config:-

<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=YOUR_SERVER;Initial
Catalog=YOUR_DATABASE;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Default .aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data
Source=.;Initial Catalog=dd;Integrated
Security=True");
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("insert into d
values('" + TextBox1.Text + "', '" + TextBox2.Text + "')", conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}

OUTPUT:
Practical No. 13 : Create a web application to insert 3 records inside the SQL database table
having following fields( Deptld, DeptName, EmpName, Salary). Update the salary for any
one employee and increment it to 15% of the present salary. Perform delete operation on 1
row of the database table.

Solution:

Step 1: Create a dept table with the following field in a database : DeptId, DeptName, EmpName, Salary
Step 2: Inset some records inside of it.
Step 3: Design the following interface in the visual studio.
Step 4: Do the coding for the designed interface.

Mysql query:-

CREATE TABLE Employee (


DeptId INT,
DeptName NVARCHAR(50),
EmpName NVARCHAR(50),
Salary DECIMAL(10,2)
);

Employee.aspx:

<h2>Insert Employee Record</h2>

DeptId: <asp:TextBox ID="txtDeptId" runat="server" /><br />


DeptName: <asp:TextBox ID="txtDeptName" runat="server" /><br />
EmpName: <asp:TextBox ID="txtEmpName" runat="server" /><br />
Salary: <asp:TextBox ID="txtSalary" runat="server" /><br />

<asp:Button ID="btnInsert" runat="server" Text="Insert Employee" OnClick="btnInsert_Click" /><br /><br />

<h2>Update Salary (15% Increment)</h2>


Employee Name: <asp:TextBox ID="txtUpdateEmpName" runat="server" /><br />
<asp:Button ID="btnUpdateSalary" runat="server" Text="Update Salary" OnClick="btnUpdateSalary_Click"
/><br /><br />

<h2>Delete Employee</h2>
Employee Name: <asp:TextBox ID="txtDeleteEmpName" runat="server" /><br />
<asp:Button ID="btnDelete" runat="server" Text="Delete Employee" OnClick="btnDelete_Click" /><br /><br
/>

<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>


Employee.aspx.cs:-

using System;
using System.Data.SqlClient;
using System.Configuration;

public partial class Employee : System.Web.UI.Page


{
string connectionString =
ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

protected void btnInsert_Click(object sender, EventArgs e)


{
using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "INSERT INTO Employee (DeptId, DeptName, EmpName, Salary) VALUES (@DeptId,
@DeptName, @EmpName, @Salary)";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@DeptId", txtDeptId.Text.Trim());
cmd.Parameters.AddWithValue("@DeptName", txtDeptName.Text.Trim());
cmd.Parameters.AddWithValue("@EmpName", txtEmpName.Text.Trim());
cmd.Parameters.AddWithValue("@Salary", txtSalary.Text.Trim());

con.Open();
cmd.ExecuteNonQuery();
lblMessage.Text = "Employee inserted successfully.";
}
}

protected void btnUpdateSalary_Click(object sender, EventArgs e)


{
using (SqlConnection con = new SqlConnection(connectionString))
{
// Increase salary by 15%
string query = "UPDATE Employee SET Salary = Salary + (Salary * 0.15) WHERE EmpName =
@EmpName";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@EmpName", txtUpdateEmpName.Text.Trim());

con.Open();
int rows = cmd.ExecuteNonQuery();
lblMessage.Text = rows > 0 ? "Salary updated successfully." : "Employee not found.";
}
}

protected void btnDelete_Click(object sender, EventArgs e)


{
using (SqlConnection con = new SqlConnection(connectionString))
{
string query = "DELETE FROM Employee WHERE EmpName = @EmpName";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@EmpName", txtDeleteEmpName.Text.Trim());

con.Open();
int rows = cmd.ExecuteNonQuery();
lblMessage.Text = rows > 0 ? "Employee deleted successfully." : "Employee not found.";
}
}
}

Web.config:

<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=YOUR_SERVER;Initial
Catalog=YOUR_DATABASE;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

OUTPUT :-

You might also like