asp.net lab
asp.net lab
T
C# and the .NET Framework. These assignments cover a range of topics, from basic
console applications to more advanced Windows Forms applications. By completing
these assignments, you will gain practical experience in writing C# code and
developing robust software applications.
Assignments:
ach assignment includes a problem statement, code, and output. The code is
E
provided as a starting point, and you are expected to modify it to meet the
requirements of the assignment.
Code:
using System;
Output:
. Program to display the first 10 natural numbers and their sum using a console
2
application.
Code:
using System;
Code:
sing System;
u
using System.Windows.Forms;
ublic AdditionForm()
p
{
// Initialize controls
txtNum1 = new TextBox();
txtNum1.Location = new System.Drawing.Point(50, 50);
txtNum1.Size = new System.Drawing.Size(100, 20);
[ STAThread]
public static void Main()
{
Application.Run(new AdditionForm());
}
}
Output:
● A Windows form will appear with two text boxes, a button, and a label.
● Enter numbers in the text boxes and click the "Add" button to see the sum in the
label.
. Write a program to convert an input string from lower to upper and upper to
4
lower case.
Code:
using System;
Output:
Code:
sing System;
u
using System.Windows.Forms;
ublic CalculatorForm()
p
{
// Initialize controls
txtDisplay = new TextBox();
txtDisplay.Location = new System.Drawing.Point(20, 20);
txtDisplay.Size = new System.Drawing.Size(200, 30);
txtDisplay.TextAlign = HorizontalAlignment.Right;
txtDisplay.Font = new System.Drawing.Font("Arial", 12);
s witch (buttonText)
{
case "C":
txtDisplay.Text = "";
firstNum = 0;
operation = "";
break;
case "+":
case "-":
case "*":
case "/":
if (txtDisplay.Text != "")
{
firstNum = Convert.ToDouble(txtDisplay.Text);
operation = buttonText;
txtDisplay.Text = "";
}
break;
case "=":
if (txtDisplay.Text != "" && operation != "")
{
double secondNum = Convert.ToDouble(txtDisplay.Text);
double result = 0;
switch (operation)
{
case "+":
result = firstNum + secondNum;
break;
case "-":
result = firstNum - secondNum;
break;
case "*":
result = firstNum * secondNum;
break;
case "/":
if (secondNum != 0)
{
result = firstNum / secondNum;
}
else
{
MessageBox.Show("Cannot divide by zero.");
txtDisplay.Text = "";
return;
}
break;
}
txtDisplay.Text = result.ToString();
firstNum = 0;
operation = "";
}
break;
default:
txtDisplay.Text += buttonText;
break;
}
}
[ STAThread]
public static void Main()
{
Application.Run(new CalculatorForm());
}
}
Output:
● A Windows form will appear with a display and buttons for numbers and
perations.
o
Use the buttons to perform calculations, and the result will be shown in the
●
display.
6. Write a program working with Page using ASP.Net.
Code:
!DOCTYPE html>
<
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Page Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblMessage" runat="server" Text="Welcome to
ASP.NET!"></asp:Label>
<br />
<asp:Button id="btnClick" runat="server" Text="Click Me"
OnClick="btnClick_Click" />
</div>
</form>
</body>
</html>
using System;
Output:
● When the page loads, it will display "Welcome to ASP.NET!"
● When the "Click Me" button is clicked, the text will change to "You clicked the
button!"
7. Write a program working with forms using ASP.NET.
Code:
!DOCTYPE html>
<
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Form Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblName" runat="server" Text="Name:"></asp:Label>
<asp:TextBox id="txtName" runat="server"></asp:TextBox>
<br />
<asp:Label id="lblEmail" runat="server" Text="Email:"></asp:Label>
<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<br />
<asp:Button id="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click" />
<br />
<asp:Label id="lblMessage" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Code Behind (FormDemo.aspx.cs):
using System;
Output:
● The page will display labels and text boxes for entering name and email.
● After entering the details and clicking the "Submit" button, the entered name and
email will be displayed.
8. Write a program to connectivity with Microsoft SQL database.
Code:
sing System;
u
using System.Data.SqlClient;
hile (reader.Read())
w
{
Console.WriteLine($"{reader["column1_name"]} -
{reader["column2_name"]}");
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
finally
{
Console.ReadLine();
}
}
}
}
Output:
Note:
● Replace "your_server_name", "your_database_name", "your_user_id", and
" your_password" with your actual SQL Server credentials.
Replace "your_table_name", "column1_name", and "column2_name" with your
●
actual table and column names.
9. Write a program to access data source through ADO.NET.
Code:
sing System;
u
using System.Data.SqlClient;
using System.Data;
Output:
Note:
● Replace "your_server_name", "your_database_name", "your_user_id", and
" your_password" with your actual SQL Server credentials.
Replace "your_table_name", "column1_name", and "column2_name" with your
●
actual table and column names.
10. Write a program to display the following feedback form.
Code:
sing System;
u
using System.Windows.Forms;
ublic FeedbackForm()
p
{
// Initialize controls
lblName = new Label();
lblName.Text = "Name:";
lblName.Location = new System.Drawing.Point(20, 20);
lblName.Size = new System.Drawing.Size(80, 20);
t xtName.Text = "";
txtEmail.Text = "";
txtFeedback.Text = "";
}
[ STAThread]
public static void Main()
{
Application.Run(new FeedbackForm());
}
}