Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't figure out what I am doing wrong. I am trying to read multiple rows of data into the same textbox. I am using a Datareader. let me explain what I am trying to do.

I am creating a customer notes/comments section in my application. I want to store notes/comments about customer service conversation. I am storing them with the date & time the comment was added. This is stored in a Access database. I want to retrieve the data and display it on a winform. I have choosen a multi-line textbox. I can get the data to display in the textbox but it only displays the last record of a customer that has multiple records.

I'm not sure if it is possible, is there a better way or what am I missing.

What I have tried:

CustomerNotes table:

CusNumber Date/Time Comment
1350 3/29/2019 8:52:42 AM Customer is wanting a refund.
1376 8/23/2018 10:52:42 PM They were not happy with the product.
1350 4/4/2019 2:43:11 PM We issued a refund.


Textbox result that I want:

3/29/2019 8:52:42 AM
Customer is wanting a refund.

4/4/2019 2:43:11 PM
We issued a refund.

My code:
VB
Try
            Dim command = New OleDb.OleDbCommand
            Call connection()
            Dim drCustomerNotes As OleDbDataReader = Nothing

            With cm
                .Connection = con
                .CommandText = "SELECT * FROM tblCusNotes WHERE fldCusNotesAcctNo = @CusAcctNo"
                Try
                    .Parameters.Clear()
                    .Parameters.Add("@CusAcctNo", OleDbType.Integer).Value = tbCusAcctNo.Text
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Critical)
                End Try
                drCustomerNotes = .ExecuteReader
            End With
            While drCustomerNotes.Read()

                CusNotesTemp = drCustomerNotes("fldCusNotesDateEntered").ToString
                    CusNotesTemp = CusNotesTemp + vbCrLf + drCustomerNotes("fldCusNotesText").ToString + vbCrLf
                tbCusNotes.Text = CusNotesTemp
                CusNotesfound = True
                End While

            'If CusNotesfound = True Then MsgBox("Cus Notes Info Found.", MsgBoxStyle.Information)
            If CusNotesfound = False Then MsgBox("Cus Notes Info Not Found.", MsgBoxStyle.Information)
            drCustomerNotes.Close()


Textbox result that I get with the above code:

4/4/2019 2:43:11 PM
We issued a refund.
Posted
Updated 3-May-19 18:36pm
Comments
[no name] 3-May-19 22:27pm    
tbCusNotes.Text = CusNotesTemp

You're not concatenating the textbox Text. Clear it at the start of the rdr loop; concatenate in the inner loop.

1 solution

Maybe something like below? Move the textbox outside the loop, concatenating the string inside the loop, then bind it to the textbox
VB
CusNotesTemp = ""
While drCustomerNotes.Read()

       ' CusNotesTemp = drCustomerNotes("fldCusNotesDateEntered").ToString
            CusNotesTemp = CusNotesTemp + drCustomerNotes("fldCusNotesDateEntered").ToString + vbCrLf + drCustomerNotes("fldCusNotesText").ToString + vbCrLf
       ' tbCusNotes.Text = CusNotesTemp
        CusNotesfound = True
        End While

tbCusNotes.Text = CusNotesTemp


You can also try concatenate the string with StringBuilder class.
Using the StringBuilder Class in .NET | Microsoft Docs[^]
 
Share this answer
 
Comments
bgcwaterman 5-May-19 8:34am    
Thanks to all that did the trick. I did some reading on the StringBuilder and it is what I need to use. I will put that in the code. Thanks again...

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