⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elect.aspx.cs

📁 学生网上选课系统,实现了学生的在线选课和教师课程的上传
💻 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 _Default : System.Web.UI.Page 
{
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        string constr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        conn = new SqlConnection(constr );
       
        if (!this.IsPostBack)
        {
            BindGrid();
        }
    }

    public void Data_Page(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        catch { }
    }
    protected void BindGrid()
    {
        string sql = "SELECT Cource.courceName,type.typename, Teacher.teaName, Cource.courceTime, Cource.courceAddress,Cource.courceId,Cource.credit,Cource.xueshi,Cource.courcerenshu,Cource.renshu,Teacher.teaid  FROM Cource,Teacher,Elect,type where Elect.teaID=Teacher.teaID and Elect.courceID=Cource.courceID and type.typeID=Cource.typeID and Elect.stuID='" + Session["userName"].ToString() + " '";

        DataSet myds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
        myda.Fill(myds);
        GridView1.DataSource = myds;
        GridView1.DataBind();
    }
   
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {


        //int index = Convert.ToInt32(e.RowIndex);            
        //    DataKey key = GridView1.DataKeys[index];            
               
            string courceID = GridView1.DataKeys[e.RowIndex].Values[0].ToString().Trim();
            string teaID = GridView1.DataKeys[e.RowIndex].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();
                string mysql = "update cource set renshu=renshu-1 where courceID='" + courceID + "'";
                SqlCommand cm = new SqlCommand(mysql, conn);
                cm.ExecuteNonQuery();
                Response.Write("<script>alert('退选成功。')</script>");
                if (conn.State.ToString() == "Open")            
                    conn.Close();
            }
            catch (Exception ex)                                
            {
                Response.Write("数据库错误,错误原因:" + ex.Message);
                Response.End();
            }
            BindGrid();

        }
    } 


    


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -