Q 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.
Solution:
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);
}
}
}
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.
Solution:
<%@ 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 = "";
}
}
}
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).
Solution:
Step 1 :- Create Database file of Employee, with fields DeptId, DeptName, EmpName,
Salary..
Step 2: Insert some records inside of it.
Step 3: Create an new project in visual studio and add GridView Control in the form.
Step 4:- bind the database file of employee and its fields to the GridView control.
Setp 5: Execute the program.