【问题标题】:Only display the candidate belong to same faculty with the user仅显示与用户属于同一教师的候选人
【发布时间】:2021-02-01 20:22:12
【问题描述】:
protected void loadCandidate()
    {
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select studentID ,name from candidate ", con);
        MySqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows == true)
        {
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
    }

Candidate database

Voter database

我正在使用 gridview 来显示候选人。我想显示基于教师的网格视图(现在网格视图正在显示所有候选人)。 When the voter login to thier account, if the voter belong to faculty MCLR, then the gridvire will only display the candidate belong to faculty MCLR, all other candidate belong to other faculty will not be shown.

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    将您的数据库对象保持在使用它们的方法的本地。其中许多需要关闭和处置。 using blocks 会为你解决这个问题。

    在使用.Execute... 方法之前,不要打开您的连接。可用的连接数量有限,应尽快将它们返回到池中。

    我不得不猜测您的登录代码可能是什么样子。我假设 StudentID 是一个整数字段。 始终使用参数以避免 Sql 注入。我使用了.ExecuteScalar(),因为我们只需要一条数据,即与学生相关联的教师。

    loadCandidate 方法中,我使用DataTable 来保存数据,因为我不确定DataReader 是否可以是DataSourceGridView。在 Select 字符串中,我们在 Where 子句中使用了一个参数,该参数的值是我们在登录时设置的。

        private string conStr = "Your connection string";
        private string facultyID = "";
        protected void login()
        {
            using (MySqlConnection con = new MySqlConnection(conStr))
            using (MySqlCommand cmd = new MySqlCommand("Select Faculty From Voter Where StudentID = @ID and Name = @Name;", con))
            {
                cmd.Parameters.AddWithValue("@ID", (int)txtID.Text);
                cmd.Parameters.AddWithValue("@Name", txtName.Text);
                con.Open();
                facultyID = cmd.ExecuteScalar().ToString();
            } //Connection and Command are closed and disposed here.
        }
        protected void loadCandidate()
        {
            DataTable dt = null;
    
            using (MySqlConnection con = new MySqlConnection(conStr))
            using (MySqlCommand cmd = new MySqlCommand("select studentID ,name from candidate Where Faculty = @FacultyID ", con))
            {
                cmd.Parameters.AddWithValue("@FacultyID", facultyID);
                con.Open();
                dt.Load(cmd.ExecuteReader());
            } //Connection and Command are closed and disposed here.
                GridView1.DataSource = dt;
                GridView1.DataBind();
        }
    

    【讨论】:

    • 显示txt.Name在当前内容中不存在。
    • 好吧,我说我必须猜你的登录密码。我假设您会使用名称和 ID 的文本框。我给了他们描述性的名字。如果用户输入的来源不同,您将不得不更改代码。它不是用来复制和粘贴的。了解它,然后将其应用于您的情况。
    • 这不是登录页面,这是一个名为 CandidateDetails 的页面。它只会使用 girdview 显示候选人和一个按钮列表允许用户投票给候选人,我想根据用户教师显示候选人(如果用户属于 MCLR,只显示候选人属于 MCLR )
    • @KekwYc 好吧,你登录学生做了什么?
    • 用户输入id和密码登录,登录后页面很多。但是,当他们打开 CandidateDetails 页面时。我想根据用户教师显示候选人详细信息
    【解决方案2】:

    如果您需要选民登录时间,请保存他的教员。可变 然后你使用你的变量 例如保存静态变量登录时间他的教师

     protected void loadCandidate()
            {
                con.Open();
                MySqlCommand cmd = new MySqlCommand(""select studentID ,name from Voter as v 
                                                    inner join candidate  as c
                                                   v.StudentID=c.StudentID where 
                                                   v.Faculty='"+yourvariablename+"' " ", con);
                MySqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows == true)
                {
                    GridView1.DataSource = dr;
                    GridView1.DataBind();
                }
            }
    

    【讨论】:

    • 我怎样才能将选民学院保存到一个变量中?也许使用会话?因为用户登录时只需要输入学生ID和密码。
    • v.Faculty='"+yourvariablename+"' " " 在任何情况下都不要这样做 stackoverflow.com/questions/14376473/…
    • 知道如何显示选民属于 MCLR 候选人属于 MCLR 学院,所有其他候选人属于其他学院不会显示?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多