I have problem when I was trying to insert values of generate-able checkbox into a SQL Server database using ASP.NET and C#.
The problem happens when I want to take the checkbox ID because the ID can be unlimited. I had run this code below, but whenever I clicked submit button, it just refreshing the page.
Here is my ASP.NET markup:
<div class="panel-body">
<div class="col-lg-1">
<div id="kolom_a">
<input type="checkbox" id="q1" runat="server" class="form-control"/>
<input type="checkbox" runat="server" id="q2" class="form-control"/>
<input type="checkbox" runat="server" id="q3" class="form-control"/>
</div>
</div>
<div class="col-lg-11">
<div id="kolom_b">
<input type="text" class="form-control" runat="server" id="a1"/>
<input type="text" runat="server" class="form-control" id="a2"/>
<input type="text" runat="server" class="form-control" id="a3"/>
</div>
</div>
<input type="hidden" id="qty" value="3"/>
<div class="row">
<div class="col-lg-12" style="padding-top:15px;">
<div class="col-lg-4"></div>
<div class="col-lg-2">
<input type="reset" onclick="kolom_a()" class="btn btn-default btn-block btn-group-lg" value="Tambah">
</div>
<div class="col-lg-2">
<input type="button" onclick="hapus_a()" class="btn btn-default btn-block btn-group-lg" value="Hapus">
</div>
<div class="col-lg-4"></div>
</div>
</div>
<script>
function kolom_a() {
var qty = document.getElementById('qty').value;
var new_qty = parseInt(qty, 10) + 1;
if (new_qty < 3) {
new_qty = 3;
}
document.getElementById('qty').value = new_qty;
var x = document.createElement("INPUT");
x.setAttribute("type", "checkbox");
x.setAttribute("name", "type");
x.setAttribute("class", "form-control");
x.setAttribute("id", 'q' + new_qty);
x.setAttribute("runat", "server");
document.getElementById("kolom_a").appendChild(x);
var x = document.createElement("INPUT");
x.setAttribute("type", "text");
x.setAttribute("class", "form-control");
x.setAttribute("id", 'a' + new_qty);
x.setAttribute("runat", "server");
document.getElementById("kolom_b").appendChild(x);
}
function hapus_a() {
var qty = document.getElementById('qty').value;
var new_qty = parseInt(qty, 10) - 1;
if (new_qty < 3) {
new_qty = 3;
}
document.getElementById('qty').value = new_qty;
var hapus = document.getElementById("kolom_a");
hapus.removeChild(hapus.childNodes[new_qty]);
var hapus = document.getElementById("kolom_b");
hapus.removeChild(hapus.childNodes[new_qty]);
}
</script>
</div>
<asp:Button ID="simpan" runat="server" class="btn btn-primary btn-block btn-group-lg" text="Simpan" OnClick="simpan_Click"/>
Here is my code behind:
private string koneksi = WebConfigurationManager.ConnectionStrings["Databaseku"].ConnectionString;
protected void simpan_Click(object sender, EventArgs e)
{
int qty = Convert.ToInt32(qty = 0);
string base64Guid = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
for (int i = 0; i < qty; i++)
{
string jawaban = string.Empty;
string SQL = "INSERT INTO question (id_question, type, title, category, feedback, points, columns) VALUES ('" + base64Guid + "','Benar Salah','" + judul.Text + "','" + kategori.Value + "', '" + umpan.Text + "', '" + poin.Text + "', '" + qty + "')";
string SQL2 = "INSERT INTO question_type_match (id_questionmatch, question, answer) VALUES ('" + base64Guid + "','q' '" + i + "','a' '" + i + "')";
SqlConnection conn = new SqlConnection(koneksi);
SqlCommand comm = new SqlCommand(SQL, conn);
SqlCommand comm2 = new SqlCommand(SQL2, conn);
try
{
conn.Open();
comm.ExecuteNonQuery();
comm2.ExecuteNonQuery();
Response.Redirect("../daftarpertanyaan.aspx");
}
catch (Exception)
{
throw;
}
finally
{
conn.Close();
}
}
}