Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Good day

I have the following table:
ID                   Paragraph

1                    Peter is walking in the garden.
2                    Jackes is playing with the ball.


I want to display the two records(Paragraphs) on seperate labels. I have used Idatareader to read the values from the table.

My problem is, it's displaying only the last record("Jackes is playing with the ball") on both lables. I want to display "Peter is walking in the garden" on label1 and "Jackes is playing with the ball" on label2, or vice versa.

What is wrong with the code?

Here is my code

//Method inside a class
public IDataReader Showrecentnews()
   {
       IDataReader reader = null;
       SqlDatabase sqldb = new SqlDatabase(clsTsogoSafe.Instance.connectionstring);
       reader = sqldb.ExecuteReader("PSELRECENTNEWS", new object[] { });
       return reader;

   }


//Calling the method in my main unit

protected void Button1_Click(object sender, EventArgs e)
   {
       clsDBCalls objDBcalls = new clsDBCalls();
       IDataReader reader = objDBcalls.Showrecentnews();
       while (reader.Read())
       {
          Label1.Text = reader["Paragraph"].ToString();
          Label2.Text = reader["Paragraph"].ToString();
       }
       reader.NextResult();
   }


Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 16-May-12 14:22pm    
If it from Microsoft.Practices?
--SA
JacoBosch 16-May-12 14:51pm    
Nope. Hahahahahha.
Sergey Alexandrovich Kryukov 16-May-12 14:56pm    
It's always good to indicate fully-qualified type names when asking such questions, to avoid ambiguity.
--SA
JacoBosch 16-May-12 14:59pm    
Thanks for the tip. I will remember that :-)
Sergey Alexandrovich Kryukov 17-May-12 16:20pm    
You are welcome. Please do.
--SA

1 solution

Wow!.. where did you get it? OK, here is a short article showing how to do such things correctly:
Using ADO.NET for beginners[^].

Basically, in your case use System.Data.SqlClient.SqlCommand.ExecuteReader:
http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx[^].

You don't need to use NextResult (why?!).

—SA
 
Share this answer
 
v3
Comments
JacoBosch 16-May-12 14:52pm    
Thanks. I will have a look at the articles. I wrote it myself.
Sergey Alexandrovich Kryukov 16-May-12 14:54pm    
Which one did you write?
--SA
JacoBosch 16-May-12 15:04pm    
Both :-)

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