Part 7. Admin Panel (Adding Students) Part - VI
In this Attendance Management System Project's video, you are going to see how an admin can add students.
Source Code Starts Here :-
Student.aspx
<center>
<div style="background-image: url('Images/background1.jpg'); width: 1200px">
<table align="center" style="height: 340px; width: 392px">
<tr>
<td colspan="2" align="center">
<h2>
Add Student</h2>
<br />
</td>
</tr>
<tr>
<td>
<b>First Name: </b></td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="197px" Height="41px" placeholder="Student's First Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Fist Name is empty" ForeColor="Red" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Name should be in Characters"
ForeColor="Red" ValidationExpression="^[A-Za-z]*$">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Last Name: </b></td>
<td>
<asp:TextBox ID="TextBox5" runat="server" Width="197px" Height="41px" placeholder="Student's Last Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox5" ErrorMessage="Last Name is empty" ForeColor="Red" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox5" ErrorMessage="Name should be in Characters"
ForeColor="Red" ValidationExpression="^[A-Za-z]*$">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Roll_No: </b></td>
<td>
<asp:TextBox ID="TextBox4" runat="server" Width="197px" Height="41px"
TextMode="Number" placeholder="Student Roll_No."></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Roll No. is empty"
ForeColor="Red" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Invalid Phone Number"
ForeColor="Red" ValidationExpression="[0-9]{7}">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Email: </b></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="197px" Height="41px"
TextMode="Email" placeholder="Student Email_Id"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Email is empty"
ForeColor="Red" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Contact_No: </b></td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Width="197px" Height="41px"
TextMode="Number" placeholder="Student Phone_No."></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Contact No is empty"
ForeColor="Red" >*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Invalid Phone Number"
ForeColor="Red" ValidationExpression="[0-9]{10}">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Course: </b></td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Height="40px" Width="197px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="DropDownList2" ErrorMessage="Select Course "
ForeColor="Red" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Year: </b></td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
Height="40px" Width="197px" >
<asp:ListItem Value="0" >Select Year</asp:ListItem>
<asp:ListItem Value="1">First Year</asp:ListItem>
<asp:ListItem Value="2">Second Year</asp:ListItem>
<asp:ListItem Value="3">Third Year</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="DropDownList3" ErrorMessage="Select Year"
ForeColor="Red" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Semester: </b></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="40px" Width="197px">
<asp:ListItem Value="0" >Select Semester</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="Select Semester"
ForeColor="Red" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Add" Font-Bold="True"
Height="47px" Width="86px" Font-Size="Medium"/></td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Font-Bold="True"></asp:Label></td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
<br />
</div>
</center>
Subject.aspx.cs
string str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DrpDwnCourse();
ShowStudent();
}
}
private void DrpDwnCourse()
{
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Course",con);
DropDownList2.DataSource = cmd.ExecuteReader();
DropDownList2.DataTextField = "CourseName";
DropDownList2.DataValueField = "CID";
DropDownList2.DataBind();
con.Close();
DropDownList2.Items.Insert(0,"Select Course");
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList2.SelectedValue == "Select Course" && DropDownList3.SelectedValue == "0" && DropDownList1.SelectedValue == "0")
{
Label1.Text = "Please select all the fields.";
Label1.ForeColor = System.Drawing.Color.Red;
}
else if (DropDownList2.SelectedValue == "Select Course" || DropDownList3.SelectedValue == "0" || DropDownList1.SelectedValue == "0")
{
Label1.Text = "Please select all the fields.";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlConnection con2 = new SqlConnection(str);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from Student where Email='" + TextBox2.Text.ToString() + "' ", con2);
DataTable dt = new DataTable();
sda1.Fill(dt);
if (dt.Rows.Count == 1)
{
Label1.Text = "Entered EmailID is already existing.";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
string FName = TextBox1.Text;
string LName = TextBox5.Text;
string FullName = FName + LName;
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("insert into Student(SName,Roll,Email,Phone,Course,Year,Sem) values(@1,@2,@3,@4,@5,@6,@7)", con);
con.Open();
cmd.Parameters.AddWithValue("@1", FullName);
cmd.Parameters.AddWithValue("@2", TextBox4.Text);
cmd.Parameters.AddWithValue("@3", TextBox2.Text);
cmd.Parameters.AddWithValue("@4", TextBox3.Text);
cmd.Parameters.AddWithValue("@5", DropDownList2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@6", DropDownList3.SelectedItem.Text);
cmd.Parameters.AddWithValue("@7", DropDownList1.SelectedItem.Text);
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
DropDownList2.SelectedValue = "Select Course";
Label1.Text = "Added Successfully";
Label1.ForeColor = System.Drawing.Color.Green;
ShowStudent();
}
}
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
if (int.Parse(DropDownList3.SelectedValue) > 0)
{
DataTable statedt = new DataTable();
statedt.Columns.Add("SemId", typeof(int));
statedt.Columns.Add("SemName");
if (DropDownList3.SelectedValue == "1")
{
statedt.Rows.Add(0, "Select Semester");
statedt.Rows.Add(1, "Sem I");
statedt.Rows.Add(2, "Sem II");
}
if (DropDownList3.SelectedValue == "2")
{
statedt.Rows.Add(0, "Select Semester");
statedt.Rows.Add(3, "Sem III");
statedt.Rows.Add(4, "Sem IV");
}
if (DropDownList3.SelectedValue == "3")
{
statedt.Rows.Add(0, "Select Semester");
statedt.Rows.Add(5, "Sem V");
statedt.Rows.Add(6, "Sem VI");
}
DropDownList1.DataSource = statedt;
DropDownList1.DataTextField = "SemName";
DropDownList1.DataValueField = "SemId";
DropDownList1.DataBind();
if (DropDownList3.SelectedValue == "0")
{
Label1.Text = "Please select proper Semester field.";
}
}
}
private void ShowStudent()
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("select SName as Name,Course,Year,Sem as Semester,Phone,Email from Student", con);
con.Open();
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
Thanks for Visiting:)
No comments
Please do not enter any spam link in the comment box.