0

i got code from internet it consist of a database table named as TblQuestions.created a Default.aspx page with a text box name as TxtRequiredRecords & button1. when value 2 enter in the textbox & click button1 it randomly generate 2 questions.if enter 5 it randomly produce 5 questions from table.i want to add another textbox2 for entering the option.my aim is that if i enter 2 in textbox1 and C in textbox2.it will generate 2 questions randomly from only option C questions.

where i modified in my code to obtain result?

my code Default.aspx :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">`
 <title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TxtRequiredRecords" runat="server"></asp:TextBox>
        <asp:Button ID="BtnDisplayRecords" runat="server" OnClick="Button1_Click" Text="Display Records" />
        <br />
        <br />
        <asp:GridView ID="GvResults" runat="server">
        </asp:GridView>
    </div>
</form>
</body>
</html>

c# code:-

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
static int TotalRecords;
SqlConnection con;
SqlDataAdapter sqlda;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string query = "SELECT COUNT(*) AS TotalRecords FROM TblQuestions";
        DataTable dt = GetRecords(query);
        TotalRecords = Convert.ToInt32(dt.Rows[0]["TotalRecords"]);
    }

}
protected void Button1_Click(object sender, EventArgs e)
{
    bool IsInt;
    int RequiredRecords;
    string CSVData, query;
    IsInt = Int32.TryParse(TxtRequiredRecords.Text, out RequiredRecords);
    if (IsInt)
    {
        if (TotalRecords >= RequiredRecords)
        {
            CSVData = GetRandomNumbersCSV(TotalRecords, RequiredRecords);
            query = "SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY QuestionID) AS RowID,* FROM TblQuestions)TempTable WHERE RowID IN(" + CSVData + ")";
            dt = GetRecords(query);
            GvResults.DataSource = dt;
            GvResults.DataBind();
        }
        else
        {
            Response.Write("Required Records must be greater than Requried Records.");
        }
    }
    else
    {
        Response.Write("Invalid Number");
    }
}

public ArrayList RandomNumbers(int max)
{
    ArrayList lstNumbers = new ArrayList();
    Random rndNumber = new Random();
    int number = rndNumber.Next(1, max + 1);
    lstNumbers.Add(number);
    int count = 0;
    do
    {
        number = rndNumber.Next(1, max + 1);
        if (!lstNumbers.Contains(number))
        {
            lstNumbers.Add(number);
        }
        count++;
    }
    while (count <= 10 * max);
    return lstNumbers;
}

public string GetRandomNumbersCSV(int max, int req)
{
    string CSV = "";
    ArrayList lstNumbers = RandomNumbers(max);
    for (int i = 0; i < req; i++)
        CSV += lstNumbers[i].ToString() + ",";
    CSV = CSV.Remove(CSV.Length - 1);
    return CSV;
}

public DataTable GetRecords(string Query)
{
    con = GetConnection();
    con.Open();
    sqlda = new SqlDataAdapter(Query, con);
    dt = new DataTable();
    sqlda.Fill(dt);
    con.Close();
    return dt;
}

public SqlConnection GetConnection()
{
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
    return con;
}
}
2
  • And what have you tried? Commented Mar 26, 2013 at 9:56
  • Which .NET version? Any reason to use the outdated ArrayList ? Commented Mar 26, 2013 at 10:33

1 Answer 1

2

Normally, ID ranges can have gaps in them, your code would fail on that. And you are also struggling to avoid duplicates.

The correct way to do this

  • obtain a list of IDs from the table, you can add a condition for "Option C" only.
  • shuffle the IdList
  • take the first N elements
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.