Header Ads

Header ADS

Part 3. Admin Panel (Adding Courses) - Part II



In this video, you can see how admin can add courses, edit and delete it in admin panel/module using Asp.Net C#.


Source Code Starts Here :--

Course.aspx <center> <div style="background-image: url('Images/background1.jpg'); width: 1200px; height: 477px"> <table align="center" style="width: 386px"> <tr> <td colspan="2" align="center"> <h2> Add Course</h2> <br /> </td> </tr> <tr> <td> <b>Course: &nbsp;&nbsp;&nbsp;</b> </td> <td> <asp:TextBox ID="TextBox1" runat="server" Height="44px" Width="230px" CausesValidation="True" placeholder="Course Name"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ForeColor="Red" ErrorMessage="Enter Course Name"></asp:RequiredFieldValidator></td> </tr> <tr> <td colspan="2" align="center" class="style1"> <asp:Button ID="Button1" runat="server" Text="Add" Height="44px" onclick="Button1_Click" Width="80px" Font-Bold="True" /> </td> </tr> <tr> <td colspan="2" align="center"> &nbsp;</td> </tr> <tr> <td colspan="2"> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </td> </tr> </table> <div align="center"> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="CID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" PageSize="4" Height="202px" Width="257px" onpageindexchanging="GridView1_PageIndexChanging"> <Columns> <asp:TemplateField HeaderText="Course"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Eval("CourseName") %>' ></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("CourseName") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField CausesValidation="false" ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" HeaderText="Operation"/> </Columns> </asp:GridView> <br /> <br /> <br /> <br /> <br /> </div> </div> </center>
Course.aspx.cs String str = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridShow(); } } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con1 = new SqlConnection(str); SqlDataAdapter sda = new SqlDataAdapter("select * from Course where CourseName='"+TextBox1.Text.ToString()+"' ",con1); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count == 1) { Label1.Text = "This Course is Already Present"; Label1.ForeColor = System.Drawing.Color.Red; } else { SqlConnection con = new SqlConnection(str); SqlCommand cmd = new SqlCommand("insert into Course(CourseName) values('" + TextBox1.Text + "')", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Label1.Text = "Course added Successfully. "; Label1.ForeColor = System.Drawing.Color.DarkGreen; TextBox1.Text = ""; GridShow(); } } private void GridShow() { SqlConnection conn = new SqlConnection(str); SqlDataAdapter sda = new SqlDataAdapter("Select * from Course", conn); DataTable dt = new DataTable(); sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; int cId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); string CourseName = (row.FindControl("TextBox2") as TextBox).Text; SqlConnection con2 = new SqlConnection(str); con2.Open(); SqlCommand cmd1 = new SqlCommand("Update Course set CourseName=@1 where CID=@2", con2); cmd1.Parameters.AddWithValue("@1", CourseName); cmd1.Parameters.AddWithValue("@2", cId); cmd1.ExecuteNonQuery(); con2.Close(); Label1.Text = "Course Updated Successful"; Label1.ForeColor = System.Drawing.Color.DarkGreen; GridView1.EditIndex = -1; } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; GridShow(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; GridShow(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int CourseId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]); SqlConnection con1 = new SqlConnection(str); con1.Open(); SqlCommand cmd1 = new SqlCommand("Delete from Course where CID=@1", con1); cmd1.Parameters.AddWithValue("@1", CourseId); cmd1.ExecuteNonQuery(); con1.Close(); Label1.Text = "Course Deleted Successful"; Label1.ForeColor = System.Drawing.Color.DarkGreen; GridShow(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GridShow(); }



Thanks for visiting:)



No comments

Please do not enter any spam link in the comment box.

Powered by Blogger.