Solved

Adding values to a ListBox

Posted on 2007-11-26
14
178 Views
Last Modified: 2010-04-15
Hey I have a ListBox whose values should be added and displayed.I could see the values being added on the code behind but the values are not being displayed instead its method is being called.For example if a particular user registers for 6 products then the names of the products should be displayed in the list box.I used List interface generic collection to populate th ID and Name of the product....but only should be displayed.....It is a winforms Application
What can I do ?
Help is appreciated!
0
Comment
Question by:onebite2
  • 7
  • 7
14 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20351107
not sure about your question. The listbox accept any object as a item, the text show for each object is it ToString() implementation. So, add the objects to the listbox, and override the ToString() method for each object class if you want to show anything different
0
 

Author Comment

by:onebite2
ID: 20351334
Hey Jaime ,

I did the same way to append here goes my code,At the last you can see
  productList.Add(product); which adds the products when I debug the, product holds=the value of the method "products registered"
 I can see all the names correctly in the code behind but the listbox display shows only the method names instead of the Name....Can you tell me what was my mistake?????I have been trying to figure out since long time........

Form1.cs
private List<UserData.ProductsRegistered> proreg;

        private void ProductsRegistered(string UserName, string productID,string Name)
        {
            proreg =userdata.GetRegisteredProducts(UserName, productID,Name);

                     
            listBox1.DataSource = proreg;
            listBox1.DisplayMember =Name;
                                 
        }

UserData.cs:

public class ProductsRegistered
    {
        public string ProductID ;
        public string Name;
    }
 
    public List<ProductsRegistered> GetRegisteredProducts(string UserName, string ProductID,string Name)
    {
 
        List<ProductsRegistered> productList = new List<ProductsRegistered>();
 
        string commandText =
            string.Format("SELECT DISTINCT a.UserID,a.ProductID,b.[Name] as Name" +
            "  FROM myacc_userregisteredProducts a " +
            "    INNER JOIN InstallationProduct b ON a.ProductID=b.[ID]" +
            "    INNER JOIN myacc_users c ON a.UserID=c.[ID]" +
            "  WHERE c.UserName ='{0}' ", UserName);
               
       // string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
       using (SqlConnection connection = new SqlConnection("server=Intranet9; database=svc; uid=sa; pwd=!galore"))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(commandText, connection))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ProductsRegistered product = new ProductsRegistered();

                        // product.ProductID = int.Parse(reader["ProductID"].ToString());
                        product.ProductID = reader["ProductID"].ToString();
                        product.Name = reader["Name"].ToString();

                        productList.Add(product);
                                               
                   }
                    return productList;
                }
            }
        }
 
    }

Thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20351370
>>...but the listbox display shows only the method names instead of the Name
Which method names?
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:onebite2
ID: 20351564
It is showing :MyAccountConsole.UserData.ProductsRegistered+ProductsRegistered in the lsit box instead of product Name...
and the value product = {MyAccountConsole.UserData.ProductsRegistered}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20351667
as far as I know, you have to define ValueMember too.
Have a look to this tutorial:
http://www.codeproject.com/csharp/ScoMListControlBinding.asp
0
 

Author Comment

by:onebite2
ID: 20351735
Okay I did declare value memeber too but still its the same ..............do u have any other method to solve this ??I will implement ur code if u have one?


Thanks!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20351847
Non-databinding approach:


public class ProductsRegistered
{
        public string ProductID ;
        public string Name;
 
        public override string ToString()
        {
                return Name;
        }
}
 
private void ProductsRegistered(string UserName, string productID,string Name)
{
            proreg = userdata.GetRegisteredProducts(UserName, productID,Name);
            listBox1.Items.Clear();
            listBox1.Items.AddRange(proreg.ToArray());
}

Open in new window

0
 

Author Comment

by:onebite2
ID: 20351956
Thanks a lot!This worked out for me by adding the ,but cna you tell me the reason behind this in a detailed way....

 public override string ToString()
        {
                return Name;
        }
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20352061
Listbox accept objects as items, a I stated earlier. Any kind of object, even distinct class for every item.
Now, the listbox has to show some text for every object it contains, but what to show?
Every .net object has a default or overriden ToString() method. The listbox will call it for every item. Just override the ToString() method to show what you want.
0
 

Author Comment

by:onebite2
ID: 20352110
Thanks a lot!!!!!
0
 

Author Comment

by:onebite2
ID: 20352157
I have one more question can we override the TOstring() method tos how any number of values or just one parameter?????

I mean If I want to show even the serial number of that product and its complete license information.....what best method can I do to display everything related to taht particular productname?

Thanks!in advance.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20352203
you show anything like:
public override string ToString()
        {
                return ID + " - " + Name;
        }
0
 

Author Comment

by:onebite2
ID: 20352320
Thanks!
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20352359
I will preciate if you close this question, I have hundreds of pending questions.
Cheers,
Jaime.
0

Featured Post

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.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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