Click here to Skip to main content
12,243,752 members (43,814 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# SQL ADO.NET
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 4-Aug-12 7:46am
Edited 4-Aug-12 8:06am
Wes Aday104.2K
v2
Comments
Wes Aday 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
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..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

this is just off the top of my head. Try populating a
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:
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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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

Or put a break statement after reading first row in your while loop:
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 5 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100