Part 4. Admin Panel (Adding Subjects) - Part III
In this video, you can see how an admin can add subject based on course, year & semester in admin panel/module using Asp.Net C#
Source Code Starts Here :-
Subject.aspx
<center>
<div style="background-image: url('Images/background1.jpg'); width: 1200px">
<table align="center" style="height: 340px; width: 391px">
<tr>
<td colspan="2" align="center">
<h2>
Add Subject</h2>
<br />
</td>
</tr>
<tr>
<td>
<b>Course: </b></td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Height="40px" Width="197px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<b>Year: </b></td>
<td>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
Height="40px" Width="197px"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
<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></td>
</tr>
<tr>
<td>
<b>Semester: </b></td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" Height="40px" Width="197px">
<asp:ListItem Value="0" >Select Semester</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<b>Subject: </b></td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="197px" Height="41px"
CausesValidation="True" placeholder="Subject Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1"
ForeColor="Red" ErrorMessage="Enter Subject Name"></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" align="center">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
</table>
<br />
<br />
<br />
<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)
{
GridShow();
ShowSubject();
}
}
private void GridShow()
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from Course",con);
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "CourseName";
DropDownList1.DataValueField = "CID";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,"Select Course");
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con2= new SqlConnection(str);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from Subject where CourseName='"+DropDownList1.SelectedItem.Text+"' and Year='"+DropDownList2.SelectedItem.Text+"' and Sem='"+DropDownList3.SelectedItem.Text+"' and SubjectName='"+TextBox1.Text.ToString()+"' ",con2);
DataTable dt = new DataTable();
sda1.Fill(dt);
if (dt.Rows.Count == 1)
{
Label1.Text = "The Subject "+TextBox1.Text.ToString()+" is already present for the selected Course="+DropDownList1.SelectedItem.Text+", Year="+DropDownList2.SelectedItem.Text+" and Semester="+DropDownList3.SelectedItem.Text+".";
Label1.ForeColor = System.Drawing.Color.Red;
}
else
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("select CID from Course where CourseName='" + DropDownList1.SelectedItem.Text + "'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "Cousre");
string cousreId = ds.Tables[0].Rows[0]["CID"].ToString();
SqlConnection con1 = new SqlConnection(str);
con1.Open();
SqlCommand cmd = new SqlCommand("insert into Subject(CID,CourseName,Year,SubjectName,Sem) values (@1,@2,@3,@4,@5)", con1);
cmd.Parameters.AddWithValue("@1", cousreId);
cmd.Parameters.AddWithValue("@2", DropDownList1.SelectedItem.Text);
cmd.Parameters.AddWithValue("@3", DropDownList2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@4", TextBox1.Text);
cmd.Parameters.AddWithValue("@5", DropDownList3.SelectedItem.Text);
cmd.ExecuteNonQuery();
con1.Close();
Label1.Text = "Subject added successfully";
Label1.ForeColor = System.Drawing.Color.Green;
clear();
}
}
private void clear()
{
DropDownList1.SelectedValue = "Select Course";
//DropDownList2.SelectedValue = "Select Year";
//DropDownList3.SelectedValue = "Select Semester";
TextBox1.Text = "";
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (int.Parse(DropDownList2.SelectedValue) > 0)
{
DataTable statedt = new DataTable();
statedt.Columns.Add("SemId", typeof(int));
statedt.Columns.Add("SemName");
if (DropDownList2.SelectedValue == "1")
{
statedt.Rows.Add(1, "Sem I");
statedt.Rows.Add(2, "Sem II");
}
if (DropDownList2.SelectedValue == "2")
{
statedt.Rows.Add(3, "Sem III");
statedt.Rows.Add(4, "Sem IV");
}
if (DropDownList2.SelectedValue == "3")
{
statedt.Rows.Add(5, "Sem V");
statedt.Rows.Add(6, "Sem VI");
}
DropDownList3.DataSource = statedt;
DropDownList3.DataTextField = "SemName";
DropDownList3.DataValueField = "SemId";
DropDownList3.DataBind();
}
}
private void ShowSubject()
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter sda = new SqlDataAdapter("select CourseName as Course,Year,Sem as Semester,SubjectName as Subject from Subject", 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.