hello all,
i got project on online examination
i want to fetch data dynamically from database.
it must be like that once student loged in. he must gives answer for 1st Q. to go next Q.

when they click on any of the buttons. next Q. must be fetch dynamically from sql server.

Recommended Answers

All 8 Replies

Getting data (all of it):
1. Create a DataTable (to later get the data out of dataBase)
2. Connect to the dataBase with an appropriate connection string (using Connection class)
3. Fill dataTable, using DataAdapter class
------------

Showing data (one by one question):
4. Loop through the rows of dataTable, starting from 0 row index (1st row) and show the question from it (so from 1st row).
5. If some conditions are meat (correct answer on the question), go to next Question in "currentRow + 1", so to next row.

PS: I hope you have answers in the dataTable as well.

Can you do that, or you need any help?

with that code we can get the next Q. but buddy i want to fetch Q. dynamically as well as randomly, it should not be in sequence,.
i got stuck on it. pls anyone can tell.

use Order by NewId() in your sql fetch query it will fetch you the randomly questions.

hey,bhagawatshinde.... thanks for your reply,
can you pls give brife introducation,or any preactical example for order by newid().
thanks a lot again,
regards,
ravi

OMG, get the row count from the datatable, use the random funtion to generate a number and ref that number with the datatable row

thanks, but i could not got that, please give me some example,

regards,
ravi

Hi,
You can use query like

string QuerySelect="select * from tablename where order by NEWID()";

each time query will execute it will show you different row result set.
Hope it will help you.

Doing that means that you you have run a Query every time. Load it into a Datatable.
Then get the Datarow with the a random number you generate. Less interaction with database. Wich is better in all cases no matter the size.

This will load the datatable

private DataTable _table;
private SqlConnection _conn;

public void LoadDataTable(string _query)
        {
            using (_conn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = _conn.CreateCommand())
                {
                    cmd.CommandText = _query;
                    cmd.CommandType = System.Data.CommandType.Text;
                    _conn.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            _table = new DataTable();
                            _table.Load(reader);
                        }
                    }
                }
                _conn.Close();
            }
        }

Then to get a datarow

DataRow row = _table.Rows[RandomNumber];
Console.WriteLine(row["ColumnName"]);

I took out some of the code(not important here). This is from a static DatabaseHandler class that I use. It works perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.