Part 5. Admin Panel (Adding Teachers) Part - IV
In this Attendance Management System Project's video, you are going to see how an admin can add teachers into the database in admin panel using Asp.Net C#.
Source Code Starts Here:--
Teacher.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 Teacher</h2>
<br />
</td>
</tr>
<tr>
<td>
<b>Fist Name: </b></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="197px" Height="41px"
CausesValidation="True" placeholder="Teacher's First Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="First Name is empty" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox2" 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="TextBox4" runat="server" Width="197px" Height="41px"
CausesValidation="True" placeholder="Teacher's Last Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Last Name is empty" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox4" ErrorMessage="Name should be in Characters"
ForeColor="Red" ValidationExpression="^[A-Za-z]*$">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
<b>Email Id: </b></td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Width="197px" Height="41px"
TextMode="Email" placeholder="Teacher Email_Id"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Email ID is empty" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<b>Password: </b></td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="197px" Height="41px"
TextMode="Password" placeholder="Teacher Password"></asp:TextBox></td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Password is empty" ForeColor="Red">*</asp:RequiredFieldValidator>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Add" Font-Bold="True"
Height="47px" Width="96px" 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 />
<br />
<br />
</div>
</center>
Teacher.aspx.cs
string str = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
GridShow();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con2= new SqlConnection(str);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from Teacher where TUserID='"+TextBox3.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 = TextBox2.Text;
string Lname = TextBox4.Text;
string FullName = Fname + Lname;
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Teacher(TName,TUserID,TPassword) values(@1,@2,@3)", con);
cmd.Parameters.AddWithValue("@1", FullName);
cmd.Parameters.AddWithValue("@2", TextBox3.Text);
cmd.Parameters.AddWithValue("@3", TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "Successfully Added";
Label1.ForeColor = System.Drawing.Color.Green;
TextBox2.Text = "";
TextBox3.Text = "";
TextBox1.Text = "";
TextBox4.Text = "";
}
}
private void GridShow()
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("select TName as Name,TUserID as EmailID from Teacher", 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.