Solved

asp.net code

Posted on 2014-09-11
6
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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