Click to See Complete Forum and Search --> : asp and visualbasic.net datalist index


Metropolis12
04-01-2007, 11:29 PM
Hello:

I am trying to get the current index number from the datalist with the DataList1_ItemCreated event as shown:

Dim dlValue As Integer

Dim item As DataListItem

dlValue = item.ItemIndex ' ...................... here is where I get "null reference" error say item.ItemIndex not set to instance of object

TextBox1.text = dlValue.toString

Is my syntax correct? Any help will be appreciated

Thanks

B^3

buntine
04-02-2007, 02:47 AM
Moved to .NET forum.

Andrew Buntine.

lmf232s
04-02-2007, 08:43 AM
Well you can get this 1 of 2 ways depending on if you want an item or if you want the datakey of the Datalist.

1. Datakey
This is where you set the datakey of the datalist. I usually set this to the primary key of the record. DataKeyField="TankId"

Dim TankId As String = dlTanks.DataKeys(e.Item.ItemIndex)


2. Item Index
Depends on what sub your trying to retrieve this from but your syntax is a little off. Give this a try, I tested this in the Itemdatabound sub but should work in all of the datalist subs.

e.item.itemindex

PeOfEo
04-02-2007, 09:17 AM
Because item is null. You just created it, but it isn't a node in any list yet so it has no index. Try adding it to your list first. Really, you shouldn't even need to handle the individual nodes like this. You should be able to do pretty much everything by just creating the list and adding to it.