Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅显示与用户属于同一教员的候选人_C#_Asp.net_Gridview - Fatal编程技术网

C# 仅显示与用户属于同一教员的候选人

C# 仅显示与用户属于同一教员的候选人,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在使用gridview显示候选人。我想显示基于教员的gridview(现在gridview显示所有候选人)。当投票者登录到其帐户时,如果投票者属于教员MCLR,则gridvire将仅显示属于教员MCLR的候选人,而不显示属于其他教员的所有其他候选人。如果需要投票者登录时间,请节省其教员。变 然后你使用你的变量 示例保存静态可变登录时间 protected void loadCandidate() { con.Open(); MySqlCommand c


我正在使用gridview显示候选人。我想显示基于教员的gridview(现在gridview显示所有候选人)。当投票者登录到其帐户时,如果投票者属于教员MCLR,则gridvire将仅显示属于教员MCLR的候选人,而不显示属于其他教员的所有其他候选人。

如果需要投票者登录时间,请节省其教员。变 然后你使用你的变量 示例保存静态可变登录时间

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();
        }
    }

将数据库对象保持在使用它们的方法的本地。其中许多需要关闭和处置<代码>使用块为您解决此问题

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

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

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

 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();
            }
        }

如何将投票者参数保存到变量?也许是利用这个环节?因为当用户登录时,他们只需要输入studentID和密码。
v.Faculty='“+yourvariablename+”“”
不要这样做,在任何情况下都不知道如何显示投票者属于MCLR候选人属于Faculty MCLR,所有其他属于其他教员的候选人都不会显示?这表明当前内容中不存在txt.Name。好吧,我说我必须猜测你的登录码。我以为你会在一个文本框中输入姓名和ID。我给了他们描述性的名字。如果用户输入的源不同,则必须更改代码。它不是用来复制和粘贴的。理解它,然后将其应用于您的情况。这不是一个登录页面,这是一个称为候选详细信息的页面。它将只使用girdview显示候选人,并有一个按钮列表允许用户投票给候选人,我想根据用户教员显示候选人(如果用户属于MCLR,则仅显示候选人属于MCLR)@KekwYc那么,您是如何登录学生的?用户将输入id和密码登录。他们登录后,会有很多页面。但是,当他们打开候选详细信息页面时。我想显示基于用户的候选人详细信息
    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();
    }