Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > 1st element not displaying in a List Box

Reply
Thread Tools

1st element not displaying in a List Box

 
 
gurvar
Guest
Posts: n/a
 
      04-05-2005
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.CloseConne ction)

'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


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      04-09-2005
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:820593A3-E72A-48F2-9232-...
> 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.CloseConne ction)
>
> '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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peterborough uk shortage of 1st 1st line jobs gerry MCSE 91 03-12-2008 06:03 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
1st element of "an array of CHAR strings" arnuld C++ 21 04-03-2007 11:49 PM
Why must <configSections> be 1st element of <configuration> ? Joseph Geretz ASP .Net 2 09-30-2005 07:49 PM
populate one list box with selected values from another list box steven.cooper@infocision.com ASP .Net 1 03-12-2005 11:29 PM



Advertisments
 



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