Using the DataTextField and DataValue Field in a ListBox

Discussion in 'ASP .Net' started by l7alabeh, Sep 20, 2006.

  1. l7alabeh

    l7alabeh Guest

    Hi,

    I am trying to take from a database 3 values, concatenate the first two
    of them and insert them in a ListBox (in the DataTextField) and the 3rd
    value in the DataValueField.

    The problem is that I am able only to add the first two values
    concatenated (using the DataReader) but the DataValueField is not
    taking any new value, it is taking the value of the text.

    Can you please help me to solve this problem?
     
    l7alabeh, Sep 20, 2006
    #1
    1. Advertising

  2. What do you think your car mechanic will answer you if you call him
    complaying that the car has a problem?

    Post your code.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "l7alabeh" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to take from a database 3 values, concatenate the first two
    > of them and insert them in a ListBox (in the DataTextField) and the 3rd
    > value in the DataValueField.
    >
    > The problem is that I am able only to add the first two values
    > concatenated (using the DataReader) but the DataValueField is not
    > taking any new value, it is taking the value of the text.
    >
    > Can you please help me to solve this problem?
    >
     
    Eliyahu Goldin, Sep 20, 2006
    #2
    1. Advertising

  3. l7alabeh

    l7alabeh Guest

    This is the code:

    Sub ListBoxBind()

    connstrconf.ConnectionString = strcnnconf

    Dim NameDataReader As OleDbDataReader
    Dim NameOleDbCommand As OleDbCommand


    NameOleDbCommand = New OleDbCommand("select first_name,
    last_name, dbid from cfg_person", connstrconf)

    connstrconf.Open()
    NameDataReader = NameOleDbCommand.ExecuteReader()


    Do While (NameDataReader.Read())
    ListBox1.Items.Add(NameDataReader.GetValue(0) & " " &
    NameDataReader.GetValue(1))
    ListBox1.datavaluefield = NameDataReader.GetValue(2)
    Loop

    NameDataReader.Close()
    connstrconf.Close()
    End Sub
     
    l7alabeh, Sep 20, 2006
    #3
  4. datavaluefield is supposed to be set to the name of the field. Your code
    assigns to it the value of the field. And anyway, datavaluefield is used
    only in databinding and you are not using any databinding, you are adding
    items programatically.

    You should do something like

    ListBox1.Items.Add(New ListItem (NameDataReader.GetValue(0) & " " &
    NameDataReader.GetValue(1), NameDataReader.GetValue(2)))

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "l7alabeh" <> wrote in message
    news:...
    > This is the code:
    >
    > Sub ListBoxBind()
    >
    > connstrconf.ConnectionString = strcnnconf
    >
    > Dim NameDataReader As OleDbDataReader
    > Dim NameOleDbCommand As OleDbCommand
    >
    >
    > NameOleDbCommand = New OleDbCommand("select first_name,
    > last_name, dbid from cfg_person", connstrconf)
    >
    > connstrconf.Open()
    > NameDataReader = NameOleDbCommand.ExecuteReader()
    >
    >
    > Do While (NameDataReader.Read())
    > ListBox1.Items.Add(NameDataReader.GetValue(0) & " " &
    > NameDataReader.GetValue(1))
    > ListBox1.datavaluefield = NameDataReader.GetValue(2)
    > Loop
    >
    > NameDataReader.Close()
    > connstrconf.Close()
    > End Sub
    >
     
    Eliyahu Goldin, Sep 20, 2006
    #4
  5. l7alabeh

    l7alabeh Guest

    Thank you
    It worked!!
     
    l7alabeh, Sep 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    3
    Views:
    4,625
    tshad
    May 10, 2005
  2. Nathan Sokalski

    Combining 2 Fields Using ListBox.DataTextField

    Nathan Sokalski, Nov 9, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    8,608
    Patrick.O.Ige
    Nov 10, 2005
  3. Paul
    Replies:
    3
    Views:
    426
    Alexey Smirnov
    Jun 10, 2008
  4. Nathan Sokalski

    Combining 2 Fields Using ListBox.DataTextField

    Nathan Sokalski, Nov 9, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    154
    Patrick.O.Ige
    Nov 10, 2005
  5. Nathan Sokalski

    Combining 2 Fields Using ListBox.DataTextField

    Nathan Sokalski, Nov 9, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    152
    Patrick.O.Ige
    Nov 10, 2005
Loading...

Share This Page