In my DataList BallotQuestions are displayed and nested Repeater each candidates names are displayed under BallotQuestions. And there is also a radiobutton inside the Radiobutton, which is attached to each candidate so that when users choose a candidate by checking the Radiobutton, they can submit by clicking the “Submit” button and insert the chosen names into the database table.
I was working fine when user selects all the candidates names based on the BallotQuestions the names are inserted. BUT ALL OF A SUDDEN IT CHANGED AND NOW IT INSERTS ONLY ONE CANDIDATE NAME. Even if I select each name based on the displayed BallotQuestions and click submit, it will insert ONLY ONE NAME.
For example if there are 2 BallotQuestions (President and Secretary) and under these BallotQuestions, there are 3 names. That will make it 6 names that will be displayed. If I select each name under the 2 BallotQuestions by selecting the Radiobuton attached to the name, it is supposed to insert 2 records, but it only inserts 1 record. The issue started not too long ago. And I have been trying hard to correct it but it’s difficult because I’m not an expert.
HTML
<div id="content" style="margin: 0 auto; width: 100%;">
<asp:Label ID="AdminID" runat="server" Text="20"></asp:Label>
<asp:Label ID="Adminlbl" runat="server" Text="Cable Network News" />
<asp:Label ID="LblMail" runat="server" Text="sammy@gmail.com"></asp:Label>
<asp:Label ID="Labelurl" runat="server" Text="http://localhost:60223/vote.aspx?id=RMA1H9S4"></asp:Label>
<asp:Label ID="startlbl" runat="server" Text="4, Oct 2021 01:43 PM"></asp:Label>
<asp:Label ID="endlbl" runat="server" Text="4, Oct 2021 04:00 PM"></asp:Label>
<asp:Label ID="lblElection" runat="server" Text="Broadcasting Commission Elections 2021" ForeColor="#0a6879" Font-Names="Varela Round" Font-Size="13pt"></asp:Label>
<asp:Label ID="idvoter" runat="server" Text="PvTiheao"></asp:Label>
<asp:Label ID="voternamelbl" runat="server" Text="Samuel Willaim" />
<div class="jumbotron-fluid" style="margin: 0 auto;">
<asp:DataList runat="server" ID="dlquestion" CssClass="row" CellPadding="4" OnItemDataBound="dlquestion_ItemDataBound" Width="100%">
<ItemTemplate>
<div class="card p-3 mb5 bg-white rounded" style="border: 1px solid #d1d5d4; margin: 0 auto; padding: 10px; width: 100%; border-radius: 10px;">
<asp:Label ID="lblQuestion" Text='<%# Eval("BallotQst") %>' runat="server" Font-Bold="true" Font-Names="Roboto, sans-serif" Font-Size="16pt" ForeColor="#003366" />
<asp:Repeater ID="rptOptions" runat="server" OnItemDataBound="rptOptions_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<div runat="server" id="innerTable">
<div class="" id="middleTable">at="server" id="checkbx" style="height: 25px; width: 25px;" />--%>
<asp:RadioButton ID="rbtBallotQuestion" runat="server" />
<asp:HiddenField ID="hfBallotQuestion" runat="server" Value='<%#Eval("BallotQst")%>' />
<asp:Image ID="postimg" runat="server" Height="20%" Width="20%" />
<br />
<div>
<asp:Label ID="Label1" runat="server" Text="NAME: " Font-Bold="true" Font-Size="13pt"></asp:Label>
<asp:Label ID="candlbl" runat="server" Text='<%# Eval("Options") %>' />
</div>
<br />
<div>
<asp:Label ID="Label2" runat="server" Text="BIO: " Font-Bold="true" Font-Size="13pt"></asp:Label>
<asp:Label ID="biolabel" Text='<%# Eval("ShortBIO") %>' runat="server" />
</div>
<br />
<br />
</div>
<hr />
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button4" runat="server" CssClass="btn btn-primary navbar-btn" Text="SUBMIT YOUR BALLOT" Width="100%" OnClick="OnSubmit" />
<script type="text/javascript">
function setExclusiveRadioButton(group, current) {
regx = new RegExp(group);
for (i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i]
if (elm.type == 'radio') {
if (regx.test(elm.value)) {
elm.checked = false;
}
}
}
current.checked = true;
}
</script>
</div>
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (Session["voter"] == null)
{
// Response.Redirect("voterlogin.aspx");
}
else
{
// showdata();
}
if (!IsPostBack)
{
string query = "SELECT DISTINCT BallotQst FROM Ballots WHERE ElectionName = '" + lblElection.Text + "' AND Name = '" + Adminlbl.Text + "' AND ElectionURL = '" + Labelurl.Text + "'";
DataTable dt = GetData(query);
dlquestion.RepeatColumns = dt.Rows.Count;
dlquestion.DataSource = dt;
dlquestion.DataBind();
dlquestion.RepeatColumns = 1;
dlquestion.RepeatDirection = RepeatDirection.Vertical;
}
}
public void showdata()
{
SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;");
string sql = "SELECT * FROM Voter WHERE VoterID='" + Session["voter"] + "'";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sqldr = cmd.ExecuteReader();
if (sqldr.Read() == true)
{
}
}
protected void dlquestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string BallotQuestion = (e.Item.FindControl("lblQuestion") as Label).Text;
Repeater rptOptions = e.Item.FindControl("rptOptions") as Repeater;
string query = "SELECT '" + BallotQuestion + "' AS BallotQst,Options,ShortBIO,photo FROM Ballots WHERE BallotQst = '" + BallotQuestion + "' AND Name = '" + Adminlbl.Text + "' AND ElectionName = '" + lblElection.Text + "' AND ElectionURL = '" + Labelurl.Text + "'";
rptOptions.DataSource = GetData(query);
rptOptions.DataBind();
}
}
protected void rptOptions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem | e.Item.ItemType == ListItemType.Item)
{
Image img = e.Item.FindControl("postimg") as Image;
DataRowView dr = (DataRowView)e.Item.DataItem;
string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["photo"]);
img.ImageUrl = imageUrl;
RadioButton radio = (RadioButton)e.Item.FindControl("rbtBallotQuestion");
HiddenField hfBallotQuestion = (HiddenField)e.Item.FindControl("hfBallotQuestion");
radio.Attributes["value"] = hfBallotQuestion.Value;
radio.Attributes.Add("onclick", "setExclusiveRadioButton('" + hfBallotQuestion.Value + "', this)");
}
}
private DataTable GetData(string query)
{
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void OnSubmit(object sender, EventArgs e)
{
foreach (DataListItem question in dlquestion.Items)
{
string VoterID = idvoter.Text;
string adminName = Adminlbl.Text;
string electionName = lblElection.Text;
string ballotQuestion = (question.FindControl("lblQuestion") as Label).Text;
Repeater rptOptions = question.FindControl("rptOptions") as Repeater;
foreach (RepeaterItem option in rptOptions.Items)
{
RadioButton radio = option.FindControl("rbtBallotQuestion") as RadioButton;
if (radio.Checked)
{
string votes = ((Label)option.FindControl("candlbl")).Text;
// Cod to save the votes in database.
try
{
int user = Convert.ToInt32(AdminID.Text);
string query = "INSERT INTO votes VALUES(@Id,@Name,@Mail,@ElectionName,@StartDate,@EndDate,@ElectionURL,@BallotQuestions,@Options,@VoterID,@VoterName,@VoteDate)";
using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\BallotDB.mdf;Integrated Security = True;"))
{
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Id", user);
cmd.Parameters.AddWithValue("@Name", adminName);
cmd.Parameters.AddWithValue("@Mail", LblMail.Text.ToString());
cmd.Parameters.AddWithValue("@ElectionName", electionName);
cmd.Parameters.AddWithValue("@StartDate", startlbl.Text.ToString());
cmd.Parameters.AddWithValue("@EndDate", endlbl.Text.ToString());
cmd.Parameters.AddWithValue("@ElectionURL", lblElection.Text.ToString());
cmd.Parameters.AddWithValue("@BallotQuestions", ballotQuestion);
cmd.Parameters.AddWithValue("@Options", votes);
cmd.Parameters.AddWithValue("@VoterID", VoterID);
cmd.Parameters.AddWithValue("@VoterName", voternamelbl.Text.ToString());
cmd.Parameters.AddWithValue("@VoteDate", DateTime.Now);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
string message = "Voted Successfully";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", "alert('" + message + "');", true);
Response.Redirect(Request.Url.AbsoluteUri);
}
}
catch(Exception ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
}
}
}