only the last element?

bluechris87

New member
Joined
Jul 17, 2016
Messages
3
Programming Experience
Beginner
hi. i am new in programming and c#.i would like to load every every u_comment from microsoft access database so as to see all of them in a richtextBox
but i managed to see onyl the last element of the database.what should i do in order to fix it?

C#:
private void Form1_Load(object sender, EventArgs e)        {
            cn2.Open();
            String myquery = "Select u_comment from table1";
            OleDbCommand cmd = new OleDbCommand(myquery, cn2);
            OleDbDataReader rdr = cmd.ExecuteReader();
            
                while (rdr.Read())
                {
                    richTextBox2.Text = rdr.GetValue(0).ToString();
                }
            cn2.Close();
        }
 
Look at this code:
C#:
                while (rdr.Read())
                {
                    richTextBox2.Text = rdr.GetValue(0).ToString();
                }
The first time through that loop, that is going to set the Text of the RichTextBox to the value from the first record. What's going to happen the second time through the loop? And the third? If you replace the previous value with the new value each time, of course you're only going to see the last value when you're done. If you expect to see every value then you have to join all those values together in some way and display the combined result.
 
for example i have value u_comennt and the elements are 1)hello 2) hi i come from USA 3) My name is... .I should use sql join to diplay all lines?
 
No, you use SQL to get the records from the database. As you read those records, you concatenate the values and then display the final result of that concatenation. Think about it. If you had a list of numbers, would you take each one in turn and replaced the previous one, would you expect to end up with the sum of all the numbers or just the last number? If you wanted to end up with the sum of all the numbers then you'd have to actually add the numbers as you went. This is no different. If you expect to end up with a combination of all the text values then you actually have to combine the text values.
 
Back
Top Bottom