温馨提示:代码在线浏览功能只能做为源码浏览参考,不能展示项目的全部,如果想更进一步了解该代码请下载:Asp.net简单网络选课系统源码
当前文件路径:MyElectCourse/studentElect.aspx.cs

1using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Collections; 5
using System.Web; 6
using System.Web.Security; 7
using System.Web.UI; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.UI.HtmlControls; 11
using System.Data.SqlClient; 12
13
public partial class StudentElect : System.Web.UI.Page 14
{ 15
protected void Page_Load(object sender, EventArgs e) 16
{ 17
18
} 19
//GridView1控件的RowCommand事件 20
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 21
{ 22
if (e.CommandName == "select") //如果单击的是“选修”按钮 23
{ 24
int index = Convert.ToInt32(e.CommandArgument); //取出选修课程所在的行索引 25
DataKey key = GridView1.DataKeys[index]; //创建DataKey集合接收该行的主键 26
string courceID = GridView1.DataKeys[index].Values[0].ToString().Trim();//取出课程编号主键值 27
string teaID = GridView1.DataKeys[index].Values[1].ToString().Trim(); //取出教师编号主键值 28
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 29
string SqlStr = "insert into Elect(stuID,courceID,teaID) values('" + Session["userName"].ToString() + "','" + courceID + "','" + teaID + "')"; 30
try 31
{ 32
SqlConnection conn = new SqlConnection(connStr);//创建连接对象 33
if (conn.State.ToString() == "Closed") //如果连接关闭,打开连接 34
conn.Open(); 35
SqlCommand comm = new SqlCommand(SqlStr, conn); 36
comm.ExecuteNonQuery(); //执行插入选修课程 37
comm.Dispose(); 38
if (conn.State.ToString() == "Open") //如果连接打开,关闭连接 39
conn.Close(); 40
} 41
catch (Exception ex) //异常处理 42
{ 43
Response.Write("数据库错误,错误原因:" + ex.Message); 44
Response.End(); 45
} 46
} 47
} 48
49
//GridView2控件的RowCommand事件 50
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) 51
{ 52
if (e.CommandName == "shanchu") //如果单击的是“选修”按钮 53
{ 54
int index = Convert.ToInt32(e.CommandArgument); //取出选修课程所在的行索引 55
DataKey key = GridView1.DataKeys[index]; //创建DataKey集合接收该行的主键 56
string stuID = GridView2.DataKeys[index].Values[0].ToString().Trim(); //取出学号主键值 57
string courceID = GridView1.DataKeys[index].Values[0].ToString().Trim();//取出课程编号主键值 58
string teaID = GridView1.DataKeys[index].Values[1].ToString().Trim(); //取出教师编号主键值 59
string userName = Session["userName"].ToString(); 60
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 61
string SqlStr = "delete from Elect where stuID='" + userName + "' and courceID='" + courceID + "' and teaID='" + teaID + "'"; 62
try 63
{ 64
SqlConnection conn = new SqlConnection(connStr);//创建连接对象 65
if (conn.State.ToString() == "Closed") //如果连接关闭,打开连接 66
conn.Open(); 67
SqlCommand comm = new SqlCommand(SqlStr, conn); 68
comm.ExecuteNonQuery(); //执行插入选修课程 69
comm.Dispose(); 70
if (conn.State.ToString() == "Open") //如果连接打开,关闭连接 71
conn.Close(); 72
} 73
catch (Exception ex) //异常处理 74
{ 75
Response.Write("数据库错误,错误原因:" + ex.Message); 76
Response.End(); 77
} 78
} 79
} 80
} 81





}
}