📄 stuchoose.aspx.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class stu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] == null)//如果没有登录,转向登录界面
{
Response.Redirect("Login.aspx");
}
else
{
if (!IsPostBack)
{
bind(); //绑定GridView
bind1();
GridView2.DataKeyNames = new string[] { "Sno", "Cno", "Teaid"};
GridView2.DataBind();
}
}
txtsnum.Text = ConfigurationManager.AppSettings["Snum"];//显示可选课程数量
}
private void bind1()//绑定学生选课信息
{
int Snum = Convert.ToInt32(ConfigurationManager.AppSettings["Snum"]);//提取学生总共能选修的个数
string sqlstr = "select Name ,Depart from Student where Sno='" + Session["Username"] + "'";
string sqlstr1 = "select count(*) from SC where Sno = '" + Session["Username"] + "'";//查询该学生已经选修的课程数
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
SqlCommand comm = new SqlCommand(sqlstr, conn);
SqlCommand comm1 = new SqlCommand(sqlstr1, conn);
try
{
conn.Open();
int num = Convert.ToInt32(comm1.ExecuteScalar());//记录学生的查询数据
txtqsnum.Text = Convert.ToString(Snum - num);
SqlDataReader sdr = comm.ExecuteReader();
if (sdr.Read())
{
Name.Text = sdr.GetValue(0).ToString();//取出登陆学生的姓名
Depart.Text = sdr.GetValue(1).ToString();//取出登陆学生的系别
}
}
catch (Exception ee)
{
Response.Write(ee.Message.ToString());
}
finally
{
conn.Close();
}
if (txtqsnum.Text == "0")//当选修门数已满的时候关系选修选项
{
this.GridView1.Columns[9].Visible = false;
this.Label1.Visible = true;//当选课数量已满提示信息
}
}
private void bind()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
// string sqlstr = "SELECT Cource.* , Teacher.* FROM Cource INNER JOIN Teacher ON Cource.teaID = Teacher.teaID WHERE (Cource.Cno NOT IN (SELECT SC FROM Elect WHERE (Sno = '"+ Session["Username"] + "')))";
string sqlstr = "SELECT Course.* , Teacher.* FROM Course INNER JOIN Teacher ON Course.teaID = Teacher.teaID WHERE (Course.Cno NOT IN (SELECT Cno FROM SC WHERE (Sno = '" + Session["Username"] + "'))) or (Course.Teaid NOT IN (SELECT Teaid FROM SC WHERE (Sno = '" + Session["Username"] + "'))) ";
//
DataSet ds = new DataSet();
try
{
SqlDataAdapter da = new SqlDataAdapter(sqlstr,conn);
conn.Open();
da.Fill(ds);
GridView1.DataKeyNames = new string[] { "Cno","Teaid"};
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch(Exception ee)
{
Response.Write(ee.Message.ToString());
}
finally
{
conn.Close();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select")
{
int index = Convert.ToInt32(e.CommandArgument);
DataKey key = GridView1.DataKeys[index];
string Cno = GridView1.DataKeys[index].Values[0].ToString().Trim();
string Teaid = GridView1.DataKeys[index].Values[1].ToString().Trim();
string sqlstr = "insert into SC(Sno,Cno,Teaid) values('" + Session["Username"] + "','" + Cno + "','" + Teaid + "')";
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["web"]);
SqlCommand comm = new SqlCommand(sqlstr, conn);
conn.Open();
SqlDataReader sdr = comm.ExecuteReader();
//comm.ExecuteNonQuery();
if (sdr.Read())
{
Response.Write("<script language=javascript>alert('选课成功')</script>");
}
bind();
//GridView2.DataBind();
Response.Redirect("stuchoose.aspx");
conn.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message.ToString());
}
}
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "shanchu") //如果单击的是“选修”按钮
{
int index = Convert.ToInt32(e.CommandArgument); //取出选修课程所在的行索引
DataKey key = GridView2.DataKeys[index]; //创建DataKey集合接收该行的主键
string stuID = GridView2.DataKeys[index].Values[0].ToString().Trim(); //取出学号主键值
string courceID = GridView2.DataKeys[index].Values[1].ToString().Trim();//取出课程编号主键值
string teaID = GridView2.DataKeys[index].Values[2].ToString().Trim(); //取出教师编号主键值
string userName = Session["Username"].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string SqlStr = "delete from SC where Sno='" + userName + "' and Cno='" + 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();
bind();
//GridView2.DataBind();
Response.Redirect("stuchoose.aspx");
}
catch (Exception ex) //异常处理
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -