Solved

Populating a Listbox in c#

Posted on 2012-12-26
9
257 Views
Last Modified: 2012-12-31
I am new to .net. I am trying to populate a listbox with the following code. what am I doing wrong? The compiler complaigns about the last line saying "cannot convert a non deligate type"

      private void popList()
            {
                  var context = new AWDEntities();
                  {
                        var query = from c in context.COMMITTEES
                                          where c.COMMITTEE_ID == 5
                                          select new { c.COMMITTEE_HOUSE, c.COMMITTEE_MEMBERSHIP, c.COMMITTEE_HEARING_AGENDAS };
                        var cust = query.First();
                        ListBox1.Text = cust.ToString;
                  };
            }

      }
}
0
Comment
Question by:vcurtis
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38721749
here is a basic way to populate a listbox



List<string> MyList = new List<string>();
MyList.Add("HELLO");
MyList.Add("WORLD");
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 38721756
try this



   private void popList()
{
                  var context = new AWDEntities();
     {
                        var query = from c in context.COMMITTEES
                                          where c.COMMITTEE_ID == 5
                                          select new { c.COMMITTEE_HOUSE, c.COMMITTEE_MEMBERSHIP, c.COMMITTEE_HEARING_AGENDAS };
                        var cust = query.First();
                        ListBox1.Add(cust.ToString);      
       }
}
0
 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 333 total points
ID: 38721910
Just replace this to your last line of code
ListBox1.Add(String.IsnullorEmpty(cust.toString()))
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:vcurtis
ID: 38721982
The listbox does not have an "add" method in its intellisence
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 38721994
Try this method
Listbox.items.add
0
 

Author Comment

by:vcurtis
ID: 38722297
I tried it both ways and got errors
listbox.docx
0
 
LVL 3

Assisted Solution

by:brezniczky
brezniczky earned 167 total points
ID: 38723041
I guess based on the error message you received, the problem is a few missing chars as below:

ListBox1.Text = cust.ToString(); // mind the trailing parentheses

as ToString is treated as a method reference otherwise rather than an actual method call.
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 333 total points
ID: 38723508
Is your variable var cust  returns multiple data.
If yes then try with this

foreach (string newCust in cust)
{
    Console.WriteLine(newCust);
}

Open in new window

0
 

Author Comment

by:vcurtis
ID: 38733684
Thanks everyone, I was able to devise a solution with the  tips  you provided
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question