Solved

asp.net code

Posted on 2014-09-11
6
223 Views
Last Modified: 2014-09-12
The aspx front end just show same record in the whole list. It is supposed to be all differen brand name from the access db. Need experts to take a look codes and see what is wrong ?
Thanks

//======asp.net c# front end ============================
  ServiceContract.Service s = new ServiceContract.Service();
            test.DataSource = s.GetBrandList();
           
            test.DataBind();
//===================================================

public IEnumerable<ServiceLibs.Brand> GetBrandList()
        {
            List<ServiceLibs.Brand> str = new List<ServiceLibs.Brand>();
            ServiceLibs.Brand brand = new ServiceLibs.Brand();
            try
            {
                using (OleDbConnection Connect = new OleDbConnection(DataConnection.OleDataCon.ToString()))
                {
                    string strItems = "SELECT distinct brandname from brand where brandname <> 'N/A' and brandname <> 'All Brand' order by brandname asc;";
                    OleDbCommand cmd = new OleDbCommand(strItems, Connect);
                    Connect.Open();
                    OleDbDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        brand.name = rdr["brandname"].ToString();
                        brand.id = 1;
                        str.Add(brand);                      
                    }
                 
                }                
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Something wrong in the query/database connection: ", ex);
            }
            return str;
        }
0
Comment
Question by:ITsolutionWizard
  • 3
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40318472
Hi ITsolutionWizard;

I suspect that the class Brand represented by  ServiceLibs.Brand does not have public properties for the values being displayed in the ListBox and what you are most likely are seeing is the class name being repeatedly displayed. but this is difficult to say as you would need to post the ServiceLibs.Brand class. You have a couple of options here, you can add public properties for the fields or you can override the ToString method and return the name field.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 40318519
Why are you giving every brand a same id? i.e. brand.id = 1;
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40318579
the brand.id is just for my test. You don't have to worry about it.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40319079
Please post the brand class.
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40319382
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ServiceLibs
{
    public class Brand
    {
        public int? id { get; set; }
        public string name { get; set; }
    }
}


Brand class above
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40319410
Hi ITsolutionWizard;

Move this Brand object from the top of the code to where I show it in the code below.

while (rdr.Read())
{
    // You need to create a new Brand object for each row you read from the reader 
    // otherwise you are adding the same object over and over again.
    ServiceLibs.Brand brand = new ServiceLibs.Brand();
    brand.name = rdr["brandname"].ToString();
    brand.id = 1;
    str.Add(brand);                       
}

Open in new window

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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