使用 findcontrol 在 gridview 中查找值,并将其与数据库中的数据相辅相成

Find the value in gridview using findcontrol and comapre it with data in database

提问人:Kekw Yc 提问时间:10/20/2020 最后编辑:Kekw Yc 更新时间:10/20/2020 访问量:134

问:

 protected void LinkButton_Click(Object sender, EventArgs e)
    {
        String MyConnection2 = "Server=localhost;database=ovs;Uid=root;password=; Convert Zero Datetime=True";
        DateTime time = DateTime.Now;              // Use current time
        string format = "yyyy-MM-dd HH:mm:ss";
        string UserName4 = HttpContext.Current.User.Identity.Name;
        GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
        Label lblStudentId = (Label)grdrow.Cells[0].FindControl("lblID");
        string studentId = lblStudentId.Text;
        String query = "insert into voting (CandidateStudentID,voterStudentID,DateTime)values ('" + lblStudentId.Text + "','" + Session["UserName"].ToString() + "','" + time.ToString(format) + "')";
        foreach (GridViewRow row in GridView2.Rows)
        {
            Label lblVoter = row.FindControl("lblVoter") as Label;
            string voterID = lblVoter.Text;


            if (Session["UserName"].ToString().Equals(lblVoter.Text))
            {
                Label1.Text = "You voted before";

            }

        }
        MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
        MySqlCommand MyCommand2 = new MySqlCommand(query, MyConn2);
        MySqlDataReader MyReader2;
        MyConn2.Open();
        MyReader2 = MyCommand2.ExecuteReader();
        Label2.Text = "Thank you for You Vote";

    }


  <asp:GridView ID="GridView2" runat="server"  AutoGenerateColumns="False" Font-Size="Medium">
          <Columns>
          <asp:TemplateField HeaderText="Student ID">
  <ItemTemplate>
     <asp:Label ID="lblVoter" runat="server"   Width="150px"  Text='<%#Eval("voterStudentID") %>'/>
 </ItemTemplate>
 </asp:TemplateField>
              
          </Columns>
       </asp:GridView>


 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();
            con.Close();

            con.Open();
            MySqlCommand cmd2 = new MySqlCommand("select voterStudentID from voting  ", con);
            MySqlDataReader dr2 = cmd2.ExecuteReader();

            GridView2.DataSource = dr2;
            GridView2.DataBind();
        }
    }

gridview2 中的 StudentID 显示

我想防止数据库中的重复投票。现在我面临一个问题,当用户以1909404的 StudentID 表中的第一个用户身份登录时,当数据库中已存在1909404时,它将显示错误消息。但是,当用户以1909362的 StudentID 表中的第二个用户身份登录时,即使该用户 ID 已经存在,也不会显示错误消息。只要用户 ID 存在于数据库中,我就想显示错误消息(这意味着他们之前投票过)。

C# asp.net GridView FindControl

评论

0赞 JohnG 10/20/2020
如何获取其数据以及多久更新一次?GridView2
0赞 Kekw Yc 10/20/2020
您好,我已经更新了有关 gridview2 如何获取数据的代码。每次有用户点击投票按钮时,gridview2 都会更新。
0赞 JohnG 10/20/2020
井。。。似乎很明显问题出在了线上......,你有没有在那里放一个断点并检查了值?换句话说,如果 ID 在 中,则...也是现在吗?显然,如果它们是相同的,并且没有设置“您之前投票过”的标签......然后发生了其他事情。是否可以验证当值相同时,它会下降到语句的部分?或者可能在此代码之后被设置为其他内容?if (Session["UserName"].ToString().Equals(lblID.Text)) …GridView2Session[“UserName”]elseiflabel1.Text
0赞 Kekw Yc 10/20/2020
当用户以1909404身份登录时,Session[“UserName”] 现在将存储1909404,当它1909404尝试第二次投票时,将显示错误消息“您之前投票过”。但对于用户1909362,即使已经对使用进行了投票,也不会显示错误消息。我想知道为什么它适用于1909404而不适用于1909362。是因为 Label lblID = (Label)GridView2.Rows[0] 吗?FindControl(“lblVoter”); ?
0赞 JohnG 10/20/2020
您是否检查了我之前描述的值?它可能适用于1909404,因为值是相同的。MAY 不适用于 1909362,因为值不相同。除了最终结果之外,您似乎没有验证这一点。如前所述放置一个断点并检查值。

答:

1赞 JohnG 10/20/2020 #1

像这样改变它......

foreach (GridViewRow row in GridView2.Rows) {
  Label lblVoter = row.FindControl("lblVoter") as Label;
  if (Session["UserName"].ToString().Equals(lblVoter.Text)) {
    Label1.Text = "You voted before";
    return;
  }
}
// Since we looped through all the rows and did NOT find a match...
// Then they can vote 
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader();
Label2.Text = "Thank you for You Vote";

评论

0赞 Kekw Yc 10/20/2020
我的天啊!这是工作!非常感谢您花时间指导我并教我如何解决错误。我真的很感激!
0赞 JohnG 10/20/2020
@Kekw Yc ...很高兴你让它工作。您应该解决的最后一点,在查询字符串中,您应该始终参数化您的查询。即使您在查询中使用了 a...,也养成将“外部”世界中用作查询中字符串的任何内容参数化的习惯是明智的。LabellblStudentId.Text
0赞 Kekw Yc 10/20/2020
感谢!将尝试根据您的建议改进当前代码!