Solved

Adding values to a ListBox

Posted on 2007-11-26
14
177 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now