1st element not displaying in a List Box

Discussion in 'ASP .Net Building Controls' started by gurvar, Apr 5, 2005.

  1. gurvar

    gurvar Guest

    Hi,
    I'm trying to populate a list box with items from a db based
    on selection from a combo id.
    If there are 2 elements, the 1st is getting left out
    If there are 4 elements, teh 1st is getting left out
    Thanks in advance,

    Following is the code:
    Try

    'Response.Write(strSQlQuery)

    'Get a new datareader from our command

    myDatareader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

    'we've got our data ...now connect it to our list:

    If myDatareader.Read = True Then

    ListBox1.DataSource = myDatareader

    ListBox1.DataValueField = "Unselected_Task_ID"

    ListBox1.DataTextField = "Unselected_List_VC"

    ListBox1.DataBind()

    myDatareader.Close()

    myConnection.Close()

    ' Next myDatareader

    Else

    ListBox1.Items.Insert(0, "---No Data---")

    End If


    Catch myException As Exception

    Response.Write("An error has occured: " & myException.ToString())

    'Finally

    ' If Not myDatareader Is Nothing Then

    ' End If

    End Try
    gurvar, Apr 5, 2005
    #1
    1. Advertising

  2. gurvar

    Teemu Keiski Guest

    Hi,

    it is because you call DataReader.Read initially before binding to the list.
    That would set the cursor one step further and make you miss the first item
    (when binding, the combo/listbox calls it also).

    You would need to bind the reader without calling Read first and if you need
    to know does it have rows, use DataReader.HasRows property before binding
    (in .NET v1.1) or check the ListBoxs Combo's Items.Count after databinding
    (v1.0). If the count is 0, you know that there was nothing to bind.

    Second way is just using DataTables/DataSet when you wouldn't have these
    issues.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU


    "gurvar" <> wrote in message
    news:...
    > Hi,
    > I'm trying to populate a list box with items from a db based
    > on selection from a combo id.
    > If there are 2 elements, the 1st is getting left out
    > If there are 4 elements, teh 1st is getting left out
    > Thanks in advance,
    >
    > Following is the code:
    > Try
    >
    > 'Response.Write(strSQlQuery)
    >
    > 'Get a new datareader from our command
    >
    > myDatareader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >
    > 'we've got our data ...now connect it to our list:
    >
    > If myDatareader.Read = True Then
    >
    > ListBox1.DataSource = myDatareader
    >
    > ListBox1.DataValueField = "Unselected_Task_ID"
    >
    > ListBox1.DataTextField = "Unselected_List_VC"
    >
    > ListBox1.DataBind()
    >
    > myDatareader.Close()
    >
    > myConnection.Close()
    >
    > ' Next myDatareader
    >
    > Else
    >
    > ListBox1.Items.Insert(0, "---No Data---")
    >
    > End If
    >
    >
    > Catch myException As Exception
    >
    > Response.Write("An error has occured: " & myException.ToString())
    >
    > 'Finally
    >
    > ' If Not myDatareader Is Nothing Then
    >
    > ' End If
    >
    > End Try
    >
    >
    Teemu Keiski, Apr 9, 2005
    #2
    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. Joseph Geretz
    Replies:
    2
    Views:
    774
    Joseph Geretz
    Sep 30, 2005
  2. Gnarlodious
    Replies:
    4
    Views:
    17,578
    elizas
    May 5, 2010
  3. arnuld
    Replies:
    21
    Views:
    1,063
    Old Wolf
    Apr 4, 2007
  4. Summercool
    Replies:
    2
    Views:
    426
    Summercool
    Mar 1, 2008
  5. candide
    Replies:
    65
    Views:
    1,392
Loading...

Share This Page