Solved

asp.net code

Posted on 2014-09-11
6
221 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 62

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
 

Author Comment

by:ITsolutionWizard
ID: 40318579
the brand.id is just for my test. You don't have to worry about it.
0
DevOps Toolchain Recommendations

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

 
LVL 62

Expert Comment

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

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 62

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASCX file or a newer alternative? 1 34
application restarter  - for an exe that hangs 9 25
List<PaisEntity> - show some 1 24
Convert datetime to time string 10 19
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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