Professional Web Applications Themes

can the datasource of a drop down list be a datareader? - ASP.NET General

Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of times equivilant to the number of records returned, instead of the two columns(value and text) I have populated the datareader with. Any ideas? Thanks in advance Cheers Jake...

  1. #1

    Default can the datasource of a drop down list be a datareader?

    Hi all,

    Is it possibele to set the datasource of a dropdown list to a datareader?
    When I try to the only column I receive is a column populated by the
    datasource name repeated the amount of times equivilant to the number of
    records returned, instead of the two columns(value and text) I have
    populated the datareader with.

    Any ideas?

    Thanks in advance

    Cheers Jake


    Jake S Guest

  2. #2

    Default Re: can the datasource of a drop down list be a datareader?

    Hi,

    You have to set the DataTextField and DataValueField .

    Natty Gur, CTO
    Dao2Com Ltd.
    28th Baruch Hirsch st. Bnei-Brak
    Israel , 51114

    Phone Numbers:
    Office: +972-(0)3-5786668
    Fax: +972-(0)3-5703475
    Mobile: +972-(0)58-888377

    Know the overall picture


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  3. #3

    Default Re: can the datasource of a drop down list be a datareader?

    Hi Jake, I'm new to this to, but I spent a lot of time figuring this
    out (with help from group postings, etc.)

    Here are two subs, both use datareader (which IS better than dataset)
    if you just want to populate lists. The first one, simply populates a
    list from a db table (please note the Not IsPostBack statement,
    otherwise you will loose the user selection). The second one uses the
    first box as a filter to populate the second listbox. I used
    OleDbConnection, but you obviously can easily make this
    SQLDbConnection, etc.

    Hope these examples help you!

    Also, I included a third example to fill just a textbox with a
    datareader field from a db. You must use the Read method to select the
    first record and test for false().

    Kathy

    ******POPULATE DROPDOWN DROM DB WITH DATAREADER*******

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    If Not IsPostBack Then
    Dim Conn1 As OleDbConnection
    Dim Rdr1 As OleDbDataReader
    Dim Cmd1 As OleDbCommand
    Dim strSQL As String
    Conn1 = New OleDbConnection(strConn)
    strSQL = "SELECT DISTINCT Station FROM tblStationUsers ORDER BY
    Station"
    Cmd1 = New OleDbCommand(strSQL, Conn1)
    Conn1.Open()
    Rdr1 = Cmd1.ExecuteReader()
    cboStation.DataSource = Rdr1
    cboStation.DataBind()
    cboStation.Items.Insert(0, "Select Station")
    cboStation.SelectedIndex = 0
    Rdr1.Close()
    Conn1.Close()
    End If
    End Sub

    *****POPULATE DROPDOWN FROM DB WITH DATAREADER USING FIRST LIST
    SELECTION AS QUERY PARAMETER******

    Private Sub cboStation_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    cboStation.SelectedIndexChanged

    Dim Conn2 As New OleDbConnection()
    Dim Rdr2 As OleDbDataReader
    Dim strSQL2 As String = "SELECT DISTINCT UserName FROM tblStationUsers
    WHERE
    ([Station] = Station) ORDER BY UserName"
    Dim Cmd2 As New OleDbCommand(strSQL2, Conn2)
    Conn2 = New OleDbConnection(strConn)
    Dim prmStation As OleDbParameter = New OleDbParameter("Station",
    OleDbType.VarChar, 50)
    prmStation.Value = cboStation.SelectedItem.Value
    Cmd2.Parameters.Add(prmStation)
    Cmd2.Connection = Conn2
    Conn2.Open()
    Rdr2 = Cmd2.ExecuteReader()
    cboUser.DataSource = Rdr2
    cboUser.DataBind()
    cboUser.Items.Insert(0, "Select User")
    cboUser.SelectedIndex = 0
    Rdr2.Close()
    Conn2.Close()
    End Sub

    *****FILL TEXTBOX WITH DATAREADER RESULT******
    Private Sub cboUser_SelectedIndexChanged(ByVal sender As
    System.Object, ByVal e As System.EventArgs) Handles
    cboUser.SelectedIndexChanged

    'Get password from db to use in validation of txtpass field
    Dim Conn3 As New OleDbConnection()
    Dim Rdr3 As OleDbDataReader
    Dim strSQL3 As String = "SELECT Password FROM tblUsers WHERE
    ([UserName] = UserName)"
    Dim Cmd3 As New OleDbCommand(strSQL3, Conn3)
    Conn3 = New OleDbConnection(strConn)
    Dim prmUserName As OleDbParameter = New OleDbParameter("UserName",
    OleDbType.VarChar, 50)
    prmUserName.Value = cboUser.SelectedItem.Value
    Cmd3.Parameters.Add(prmUserName)
    Cmd3.Connection = Conn3
    Conn3.Open()
    Rdr3 = Cmd3.ExecuteReader()
    If Rdr3.Read() Then
    dbPass.Text = Rdr3("Password") 'used to compare to tblUsers
    End If
    Rdr3.Close()
    Conn3.Close()
    End Sub






    "Jake S" <reachJakehotmail.com> wrote in message news:<#VQIVu8QDHA.1712TK2MSFTNGP12.phx.gbl>...
    > Hi all,
    >
    > Is it possibele to set the datasource of a dropdown list to a datareader?
    > When I try to the only column I receive is a column populated by the
    > datasource name repeated the amount of times equivilant to the number of
    > records returned, instead of the two columns(value and text) I have
    > populated the datareader with.
    >
    > Any ideas?
    >
    > Thanks in advance
    >
    > Cheers Jake
    KathyB Guest

Similar Threads

  1. Drop Down List??
    By BrianGraziano in forum Macromedia Dynamic HTML
    Replies: 2
    Last Post: October 17th, 12:40 PM
  2. drop down list
    By stubert webforumsuser@macromedia.com in forum Dreamweaver AppDev
    Replies: 4
    Last Post: July 6th, 12:49 PM
  3. List Datasource
    By Kenny Wan in forum Coldfusion Server Administration
    Replies: 1
    Last Post: June 9th, 10:16 PM
  4. Replies: 2
    Last Post: September 11th, 07:05 PM
  5. Drop Down List?
    By Hollywood TJ in forum Macromedia Flash
    Replies: 0
    Last Post: June 27th, 02:42 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139