Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop Test engine and i want to get record one by one from Table.

Please help me to solve this problem.

Below code return last record of the table.

OleDbConnection con = new OleDbConnection();
            con.ConnectionString = ConfigurationSettings.AppSettings["connection"];
            con.Open();
            OleDbCommand cmd = new OleDbCommand("Select * from ExamPaper where TestName = @tn", con);
            cmd.Parameters.Add(new OleDbParameter("@tn", cmbtestname.Text));
            OleDbDataReader dr = cmd.ExecuteReader();
            cmbtestname.Items.Clear();

           
                while (dr.Read())
                {
                    txtquestion.Text = Convert.ToString(dr[4]);
                    radioBtn1.Text = Convert.ToString(dr[5]);
                    radioBtn2.Text = Convert.ToString(dr[6]);
                    radioBtn3.Text = Convert.ToString(dr[7]);
                    radioBtn4.Text = Convert.ToString(dr[8]);
                }
Posted
Updated 4-Aug-12 7:06am
v2
Comments
[no name] 4-Aug-12 13:05pm    
No....you are getting all of the records you are just overwriting txtquestion.text so all you see is the text from the last record.
varuncodee 4-Aug-12 13:08pm    
Thanks Wes Aday for reply... so how can i get row one by one in textbox? that is my question.

your datareader have all records. but everytime each rows are displayed in textboxes but overwritten by next row.

Use gridView to display all the records. Bind data reader as gridview datasource
 
Share this answer
 
Comments
varuncodee 4-Aug-12 13:12pm    
Thanks Santhosh .. i don't want to use girdview, I am trying to develop test engine and i want record one by one in text boxes i have next and prev button to navigate through the question's
.. please help..
this is just off the top of my head. Try populating a
C#
List<>
from the datareader and the use LINQ extensions Skip() and Take() to move one item at a time on next prev button click event. In this case, starting page index would be zero and page size would be 1.

So, something like:
C#
string item = list.Skip(currentpageindex*pagesize).Take(pagesize).Select(x=>x).FirstOrDefault()


should work. You would have to make sure that the page index is a global or a static variable that would retain its most recent value between page switches.

Hope this helps.
 
Share this answer
 
If you want only one row from your returned query then change the query itself. Try this query:
SQL
Select DISTINCT * from ExamPaper where TestName = @tn


Or put a break statement after reading first row in your while loop:
C#
while (dr.Read())
{
    txtquestion.Text = Convert.ToString(dr[4]);
    radioBtn1.Text = Convert.ToString(dr[5]);
    radioBtn2.Text = Convert.ToString(dr[6]);
    radioBtn3.Text = Convert.ToString(dr[7]);
    radioBtn4.Text = Convert.ToString(dr[8]);
    break;
}




--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900