0% found this document useful (0 votes)
50 views5 pages

Show A List of Courses From Databases (Course Enrollment System) and Apply CRUD

Show a list of courses from databases (Course Enrollment System) and apply CRUD operations using gridview control. c sharp

Uploaded by

Syed Kazmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Show A List of Courses From Databases (Course Enrollment System) and Apply CRUD

Show a list of courses from databases (Course Enrollment System) and apply CRUD operations using gridview control. c sharp

Uploaded by

Syed Kazmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Tasks

Show a list of courses from databases (Course Enrollment System) and apply
CRUD operations using gridview control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="labb13.WebForm1"


%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
Course Enrollment System
</h1><br />
<asp:GridView ID="gridService" runat="server" OnRowDeleting="gridService_RowDeleting"
DataKeyNames="id" OnRowEditing="gridService_RowEditing" OnRowUpdating="gridService_RowUpdating"
OnRowCancelingEdit="gridService_RowCancelingEdit">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace labb13
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}

protected void gridService_RowEditing(object sender, GridViewEditEventArgs e)


{
gridService.EditIndex = e.NewEditIndex;
this.BindGrid();
}

protected void gridService_RowDeleting(object sender, GridViewDeleteEventArgs e)


{
string name1 = gridService.DataKeys[e.RowIndex].Value.ToString();
string query = "Delete from Course where id = '" + name1 + "'";
string connectionstring = @"Data Source=DESKTOP-2S589NA;Initial Catalog=lab13;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
this.BindGrid();
}

protected void gridService_RowUpdating(object sender, GridViewUpdateEventArgs e)


{
GridViewRow row = gridService.Rows[e.RowIndex];
string name1 = gridService.DataKeys[e.RowIndex].Value.ToString();
string coursename = ((TextBox)row.Cells[3].Controls[0]).Text;
string teachername = ((TextBox)row.Cells[4].Controls[0]).Text;

string query = "UPDATE Course SET coursename=@coursename, teachername=@teachername WHERE id =


@Name";
string connectionstring = @"Provider=Microsoft.ACE.Sql.12.0;Data
Source=C:\Users\nouman\Documents\Register.accdb";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@coursename", coursename);
cmd.Parameters.AddWithValue("@teachername", teachername);

cmd.Parameters.AddWithValue("@Name", name1);
cmd.ExecuteNonQuery();
con.Close();
gridService.EditIndex = -1;
this.BindGrid();
}
public void BindGrid()
{
string query = "select * from Course";
string connectionstring = @"Data Source=DESKTOP-2S589NA;Initial Catalog=lab13;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionstring);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds, "Course");
con.Close();
gridService.DataSource = ds.Tables["Course"];
gridService.DataBind();
}

protected void gridService_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)


{
gridService.EditIndex = -1;
this.BindGrid();
}

}
}

You might also like