How to make math captcha in C#.net

In spare time, make captcha in math style using C# – ASP.Net. First thing first, create the database. Then show the captcha randomly. I know it’s unsecure this way. Better replace the captcha with image.

View:

  <asp:Label ID="capcay" runat="server" Text="Label"></asp:Label>
    <asp:HiddenField ID="hd_capcay" runat="server" />
    <asp:TextBox ID="a_capcay" runat="server"></asp:TextBox>
     <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button" />
     <asp:Label ID="validd" runat="server" Text="Label"></asp:Label>
     <asp:Label ID="dump" runat="server" Text="Label" Visible="false"></asp:Label>

Controller

 private RetnoTest.Library.Master.Test msc = new Library.Master.Test();
        private DbDataReader MyRead;
        protected void Page_Load(object sender, EventArgs e)
        {
            dump.Text = hd_capcay.Value;
            retrieve_capcay();
        }
  protected void retrieve_capcay()
        {
            MyRead = msc.Capcay_browse();
            hd_capcay.Value = MyRead[0].ToString();
            capcay.Text = MyRead[1].ToString();
        }
        protected void jawab_capcay() {            
            MyRead = msc.jawab_capcay(dump.Text, a_capcay.Text);
            if (MyRead.HasRows)
            {
                    validd.Text = "benar!";
                    retrieve_capcay();
            }
            else {
                validd.Text = "salah. itung lagi";
                
            }
            
            a_capcay.Text = "";
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
             jawab_capcay();            
        }

Model

   public DbDataReader Capcay_browse()
        {
            strSQL = "SELECT TOP 1 * FROM MC_Capcay order by newid()"; // menampilkan data di SQLServer secara acak
            return Helper.ExecuteReader(strSQL);
        }
        public DbDataReader jawab_capcay(string id, string jawab)
        {
            strSQL = "SELECT * FROM MC_Capcay WHERE id = '" + id + "' and jawaban = '"+ jawab +"'";
            return Helper.ExecuteReader(strSQL);
        }

How to edit view on mysql

  1. Run the following query: SHOW CREATE VIEW your_view_name
  2. Expand the options and choose Full Texts
  3. Press Go
  4. Copy entire contents of the Create View column.
  5. Make changes to the query in the editor of your choice.
  6. Add this code before your query select: CREATE OR REPLACE
  7. Press GO

Solution: Subquery Return More Than 1 Row

just add LIMIT 1 in the end of your query.

for example:

select *
from table
where id IN (multiple row query);

make it like this:

SELECT *
FROM Students
WHERE Marks = (SELECT MAX(Marks) FROM Students) --Subquery returns only 1 value

SELECT *
FROM Students
WHERE Marks IN
(SELECT Marks
FROM Students
ORDER BY Marks DESC
LIMIT 10) --Subquery returns 10 values

PS: the sample taken from stackoverflow

Implode foreach or looping value with comma

<?php
/*
$elements = array();
foreach($names as $name) {
    //do something
    $elements[] = '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(',', $elements);
*/

$elements = array();
for($i=1; $i<=5; $i++) {
    //do something
    $elements[] = $i;
}
$arraynya = implode(',', $elements);
echo $arraynya;
?>

result: 1,2,3,4,5 without comma in the end.