Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre> 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();
        }
    }


What I have tried:

The error message and successful inserted message are display at the same time. The duplicated of the vote are allowed even though Session["UserName"] is equal to lblVoter.Text
Posted
Updated 19-Oct-20 20:09pm
v2
Comments
Richard MacCutchan 20-Oct-20 3:51am    
What error message, and where does it occur?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900