?? studentelect.aspx.cs
字號:
?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class StudentElect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//GridView1控件的RowCommand事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select") //如果單擊的是“選修”按鈕
{
int index = Convert.ToInt32(e.CommandArgument); //取出選修課程所在的行索引
DataKey key = GridView1.DataKeys[index]; //創建DataKey集合接收該行的主鍵
string courceID = GridView1.DataKeys[index].Values[0].ToString().Trim();//取出課程編號主鍵值
string teaID = GridView1.DataKeys[index].Values[1].ToString().Trim(); //取出教師編號主鍵值
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "insert into Elect(stuID,courceID,teaID) values('" + Session["userName"].ToString() + "','" + courceID + "','" + teaID + "')";
try
{
SqlConnection conn = new SqlConnection(connStr);//創建連接對象
if (conn.State.ToString() == "Closed") //如果連接關閉,打開連接
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery(); //執行插入選修課程
comm.Dispose();
if (conn.State.ToString() == "Open") //如果連接打開,關閉連接
conn.Close();
}
catch (Exception ex) //異常處理
{
Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
Response.End();
}
}
}
//GridView2控件的RowCommand事件
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "shanchu") //如果單擊的是“選修”按鈕
{
int index = Convert.ToInt32(e.CommandArgument); //取出選修課程所在的行索引
DataKey key = GridView1.DataKeys[index]; //創建DataKey集合接收該行的主鍵
string stuID = GridView2.DataKeys[index].Values[0].ToString().Trim(); //取出學號主鍵值
string courceID = GridView1.DataKeys[index].Values[0].ToString().Trim();//取出課程編號主鍵值
string teaID = GridView1.DataKeys[index].Values[1].ToString().Trim(); //取出教師編號主鍵值
string userName = Session["userName"].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "delete from Elect where stuID='" + userName + "' and courceID='" + courceID + "' and teaID='" + teaID + "'";
try
{
SqlConnection conn = new SqlConnection(connStr);//創建連接對象
if (conn.State.ToString() == "Closed") //如果連接關閉,打開連接
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery(); //執行插入選修課程
comm.Dispose();
if (conn.State.ToString() == "Open") //如果連接打開,關閉連接
conn.Close();
}
catch (Exception ex) //異常處理
{
Response.Write("數據庫錯誤,錯誤原因:" + ex.Message);
Response.End();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -