0

enter image description here

I'm trying to load Data from database to RichTextBox (as image shows) in my c# windows form Project using Linq.

I don't know if I am doing it right, because the data is not loading to the RichTextBox. Please help.

This is how I'am trying to do it:

 string idNr = txtIdcardNr.Text.Trim();

 var CheckIfIdCardExist = (from u in db.Customer
                           where u.IdentityCardNr == idNr
                           select u).FirstOrDefault();
     if(CheckIfIdCardExist != null)
     {
       String template =
       @"Date\t\t{0}
       Notes\t\t{1}
       Staff\t\t{2}
       *********\t\t{3}";
              var notes = (from u in db.CustomerNotes
                           join em in db.Employee on u.StaffId equals em.EmployeeId
                           where u.CustomerId == CheckIfIdCardExist.CustomerId
                           select new {
                                         Date = u.NoteDate,
                                         notes = u.Notes,
                                         employee = em.FirstName + " " + em.LastName


                                     }).ToList();
                    foreach(var n in notes)
                    {
                        richTextBox1.Text = string.Format(template, n.Date, n.notes, n.employee);
                    }
6
  • You didn't say what this was doing that you didn't like. However, I'm proposing a stab at it in the answers. Commented Dec 27, 2018 at 0:53
  • 1
    richTextBox1.Text = obviously this line is going bring you unstuck regardless of any other problem, you'll just be adding the last note Commented Dec 27, 2018 at 0:53
  • Is the image what you WANT to see? Commented Dec 27, 2018 at 0:57
  • @Ann L. thank you for response, Yes I want to see like tha tImage Commented Dec 27, 2018 at 0:59
  • Is the problem the one that @TheGeneral pointed out -- that you're only seeing your final note? If so, I proposed a solution, below. Commented Dec 27, 2018 at 1:00

1 Answer 1

2

I'm taking a big leap here, and guessing that the primary problem is that you're not seeing all your notes, just the final one.

     var notes = (from u in db.CustomerNotes
                       join em in db.Employee on u.StaffId equals em.EmployeeId
                       where u.CustomerId == CheckIfIdCardExist.CustomerId
                       select new {
                                     Date = u.NoteDate,
                                     notes = u.Notes,
                                     employee = em.FirstName + " " + em.LastName


                                 });

     StringBuilder sb = new StringBuilder();
     foreach(var n in notes)
     {
        sb.AppendFormat(template, n.Date, n.notes, n.employee);
        sb.Append("\n");
     }
     richTextBox1.Text = sb.ToString();
Sign up to request clarification or add additional context in comments.

2 Comments

L Thank you very much. Now it's working as I want. I have deleted my \t\t and Notes , staff and **** Begins in new line as I want but but in position zero. I mean for example Notes Begins exactly after the end of Date, not longside to the left but under the end of date. I tried \n in template but did not work. But its wonderful, it's working... Thank you very much. I don't if you can see what is the problem that Notes, Staff and **** not Begins from the very left side.
It sounds as if you need a carriage return character. I don't know what that is in RTF, unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.