-1

I'm creating a booking system where a customer can enter a booking ID and see all other guests attending, I need help displaying the values from my LINQ list in a series of textboxes, any and all help would be appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace BookingCustomers
{
    public partial class BookingGuests : System.Web.UI.Page
    {
        private HotelConferenceEntities datacontext = new HotelConferenceEntities();
        private void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                try
                {
                    int id = int.Parse(BookID.Text.ToString());
                    tblBooking booking = datacontext.tblBookings.SingleOrDefault(x => x.BookingID == id);
                    tblVenue venue = datacontext.tblVenues.SingleOrDefault(x => x.VenueID == booking.Venue);
                    List<tblCustomer> customers = new List<tblCustomer>();
                    List<tblBookingGuest> guests = booking.tblBookingGuests.ToList();

                        foreach (tblBookingGuest guest in guests)
                        {
                            tblCustomer newcust = datacontext.tblCustomers.SingleOrDefault(x=>x.CustomerID == guest.CustomerID);
                            customers.Add(newcust);
                        }
                        int count = customers.Count;
                        CustFirstName.Text = Convert.ToString(customers);
4b0
  • 21,981
  • 30
  • 95
  • 142
Captain_Custard
  • 1,308
  • 6
  • 21
  • 35
  • possible duplicate of [How to display values from a LINQ list in a series of textboxes?](http://stackoverflow.com/questions/16834385/how-to-display-values-from-a-linq-list-in-a-series-of-textboxes) – L.B May 30 '13 at 12:43

2 Answers2

1

Are you trying to use a ToString() on the customers List, or is that just meant to be pseudocode?

Try using something similar to this: Convert a list to a string in C#

This will probably include giving the tblCustomer class a ToString() override, then use that to provide the strings you need to your text boxes:

class tblCustomer
{
    public override string ToString()
    {
        return this.name;
    }
}
Community
  • 1
  • 1
anthr
  • 719
  • 5
  • 13
0

Not sure what result you're looking for - do they have to be textboxes - meaning, user-editable? If not, you can just use a single label, and append each name, either a comma-delimited string or separated with "" in between each. Might not be terribly pretty, but it would work. Instead of 'customers' being a list of tblCustomer, make it a List, and add a ToString method in that loop, then do a string.join for the text box.

List<string> customers = new List<string>();
foreach (tblBookingGuest guest in guests)
{
   tblCustomer newCust = datacontext.tblCustomers.SingleOrDefault(x=>x.CustomerID == guest.CustomerID);
   customers.Add(GetCustomerString(newCust));
}
CustFirstName.Text = string.Join("</BR>", customers.ToArray);

Then later, define the GetCustomerString method.

private string GetCustomerString(tblCustomer customer)
{
   // insert logic or, alternatively, override tblCustomer's ToString instead
}
Deeko
  • 1,514
  • 9
  • 29