Покажите сообщение об ошибке и предотвратите дублирование голосования
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"); Label lblvID = (Label)GridView2.Rows[0].FindControl("lblvID"); string label1val = lblvID.Text; string studentId = lblStudentId.Text; // string id =; // string testing = this.Encrypt(id.Trim()); String query = "insert into voting (CandidateStudentID,voterStudentID,DateTime)values ('" + lblStudentId.Text + "','" + Session["UserName"].ToString() + "','" + time.ToString(format) + "')"; if (Session["UserName"].ToString().Equals(lblvID.Text)) { Label1.Text = "You voted beofre"; } else { 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"; } } <pre><asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Font-Size="Medium"> <Columns> <asp:TemplateField HeaderText="Student ID"> <ItemTemplate> <asp:Label ID="lblvID" runat="server" Width="150px" Text='<%#Eval("voterStudentID") %>'/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Что я уже пробовал:
Кнопка link принадлежит gridview 1.я получаю ошибку index out of range в окне
Label lblvID = (Label)GridView2.Rows[0].FindControl("lblvID");.