使用findcontrol在gridview中查找值,并将其与数据库中的数据进行比较

问题描述

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

StudentID display in gridview2

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

解决方法

像这样更改它。...

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";