Results 1 to 11 of 11

Thread: DB Class - Populate DropdownList

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    DB Class - Populate DropdownList

    Hi,

    I'm trying to write a DB actions class for my web project to help reduce code duplication for example I want to show a dropdownlist with countries, populated from my DB, on more than one page, so instead of writing the code in each required page I thought that if I put it in a class I could then call a function i.e. populateCountriesDDL when required.

    Does anyone know how to do this? Am I going about this the correct way or is there a better solution, with classic asp I would create an asp include file and add it in wherever needed

    I've tried referencing


    VB Code:
    1. Imports System.Web.UI
    2. Imports System.Web.UI.HtmlControls
    3. Imports System.Web.UI.WebControls

    but get error message - namespace could not be found for each of the above

    Thought I could pass the function the DDL and populate it from there?

    Any help will be greatly appreciated

    Cheers Al

  2. #2
    Fanatic Member d2005's Avatar
    Join Date
    Aug 2005
    Location
    ireland
    Posts
    620

    Re: DB Class - Populate DropdownList

    i havent a clue this is how i would go about it, IM NEW
    perhaps some experinced members could read this and help further.
    i havent done this yet but this is how im going to try it

    create a class and do a get get countries function as dataset

    public shared function getcountries() as dataset
    dim dscountries as new dataset
    dim sselect as string
    ' your select statement'
    dim dacountries as new sqldataadapter(sSelect, connection())
    dacountries.fill(dscountries, "country table")
    'connection() is a private funtion in the same class that connects to database

    then in your form code behind file - bind the ddl

    this prob didnt help but might get the ball rolling for you
    Last edited by d2005; Sep 14th, 2005 at 05:13 AM.
    it works 60% of the time, all the time.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: DB Class - Populate DropdownList

    A dropdownlist can be bound to an array. So what you need in your class is a function which will return an array containing all the countries with their values in the array. Make sense?

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: DB Class - Populate DropdownList

    D2005,

    Thanks, it's not quite what I had in mind, but as you say it's a start

    Cheers Al

  5. #5
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: DB Class - Populate DropdownList

    Create a class with a static function that accepts a DropDownList as a parameter, then populates it.

    To call it use;

    MyClass.PopulateCountries(this.DropDownCountries);

  6. #6

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: DB Class - Populate DropdownList

    Mendhak,

    Thanks for that, I did think of doing it that way but would mean on each page I have to loop through the array populating my DDL as I want to be able to set the default selected item i.e. for when a user wants to edit their profile etc. I'm told you can't set the selected item by

    VB Code:
    1. ddlCountry.SelectedIndex = "United Kingdom"

    Instead I'm doing it like this as I don't know what the index is. If there is a better/more efficient way of doing this please let me know
    VB Code:
    1. dr = myCmd.ExecuteReader()
    2.             i = 0
    3.             ddlCountry.Items.Add("[Select]")
    4.             ddlCountry.Items(i).Value = "0"
    5.  
    6.             Do While dr.Read()
    7.                 i += 1
    8.                 ddlCountry.Items.Add(Trim(CStr(dr("Country"))))
    9.                 ddlCountry.Items(i).Value = Trim(CStr(dr("Country_Code")))
    10.                 If sdefault = Trim(CStr(dr("Country"))) Then
    11.                     ddlCountry.SelectedIndex = i
    12.                 End If
    13.             Loop
    What I wanted to do was pass in a ref to a DDL and a default item and the function would take care of the rest for me.

    VB Code:
    1. MyClass.PopulateCountries(this.DropDownCountries, sDefault)

    As suggested by GlenW however I can't seem to reference

    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls
    Imports System.Web.UI.WebControls

    when I try to create my populateCountries function there is no dropdownlist in the list of classes (see bold below) -

    VB Code:
    1. Public Function PopulateCountries(byRef myDDL as [b]DropDownList[/b], optional byRef sDefault as string = "0") as boolean

    I'm sure this can be done, but being a newbie I'm just doing it incorrectly, I truely welcome all guidence

    Hopefully the above makes sense

    If not let me know

    Cheers Al

  7. #7
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: DB Class - Populate DropdownList

    Have you referenced System.Web?
    If you are writing a class utility that is not part of an asp.net project it won't be referenced automatically.
    Just right click on References in solution explorer and then click Add Reference.

  8. #8

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: DB Class - Populate DropdownList

    Glen,
    Yes I'm already referencing System.Web, but I can't find these bad boys -

    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls
    Imports System.Web.UI.WebControls
    I have an asp.net web project and I've added a class project to it.

  9. #9
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: DB Class - Populate DropdownList

    Quote Originally Posted by aconybeare
    I have an asp.net web project and I've added a class project to it.
    Have you added the System.Web reference to the new class project?

  10. #10

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: DB Class - Populate DropdownList

    Glen,

    I was just about to reply yes, when I thought I'd better check and it seems not. I've now added it in and the others are now showing okay, yet another school boy error!

    Thanks for your help, hopefully I'm on my way now

    Cheers Al

  11. #11
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: DB Class - Populate DropdownList

    Glad to be of service

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width