提问人:Kekw Yc 提问时间:10/20/2020 最后编辑:Kekw Yc 更新时间:10/20/2020 访问量:134
使用 findcontrol 在 gridview 中查找值,并将其与数据库中的数据相辅相成
Find the value in gridview using findcontrol and comapre it with data in database
问:
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();
}
}
我想防止数据库中的重复投票。现在我面临一个问题,当用户以1909404的 StudentID 表中的第一个用户身份登录时,当数据库中已存在1909404时,它将显示错误消息。但是,当用户以1909362的 StudentID 表中的第二个用户身份登录时,即使该用户 ID 已经存在,也不会显示错误消息。只要用户 ID 存在于数据库中,我就想显示错误消息(这意味着他们之前投票过)。
答:
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...,也养成将“外部”世界中用作查询中字符串的任何内容参数化的习惯是明智的。Label
lblStudentId.Text
0赞
Kekw Yc
10/20/2020
感谢!将尝试根据您的建议改进当前代码!
评论
GridView2
if (Session["UserName"].ToString().Equals(lblID.Text)) …
GridView2
Session[“UserName”]
else
if
label1.Text