get datakey from datalist

A

Andy Sutorius

Hi,

I have a datalist with a asp:button. I have onitemcommand firing when the
button is clicked. I am attempting to get the datakey of the button that was
clicked. I can retrieve a specific datakey with the following: int
intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
retrieve the datakey of the button clicked? I have tried this
unsuccessfully: int intProductID = Convert.ToInt16(dlstProducts.DataKeys[e])

Thanks,

Andy
 
A

Andy Sutorius

Hi Karl,

Why should it be ToInt32?

Andy


Karl Seguin said:
use e.Item.ItemIndex to get the index of the item

dlstProducts.DataKeys[e.Item.ItemIndex]

also, any particular reason you are using ToInt16 instead of 32?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Andy Sutorius said:
Hi,

I have a datalist with a asp:button. I have onitemcommand firing when the
button is clicked. I am attempting to get the datakey of the button that was
clicked. I can retrieve a specific datakey with the following: int
intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
retrieve the datakey of the button clicked? I have tried this
unsuccessfully: int intProductID = Convert.ToInt16(dlstProducts.DataKeys[e])

Thanks,

Andy
 
K

Karl Seguin

in my opinion yes...assuming ur developing on 32-bit system...

a c# int (or vb.net integer) maps to Int32 on a 32bit system so it's what
you'll get the most performance out of...

Int16 is only useful when you have very large chunks of data (large int
arrays) and you want to consume less space....

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Andy Sutorius said:
Hi Karl,

Why should it be ToInt32?

Andy


Karl Seguin said:
use e.Item.ItemIndex to get the index of the item

dlstProducts.DataKeys[e.Item.ItemIndex]

also, any particular reason you are using ToInt16 instead of 32?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Andy Sutorius said:
Hi,

I have a datalist with a asp:button. I have onitemcommand firing when the
button is clicked. I am attempting to get the datakey of the button
that
was
clicked. I can retrieve a specific datakey with the following: int
intProductID = Convert.ToInt16(dlstProducts.DataKeys[0]) but how do I
retrieve the datakey of the button clicked? I have tried this
unsuccessfully: int intProductID = Convert.ToInt16(dlstProducts.DataKeys[e])

Thanks,

Andy
 
A

Andy Sutorius

Karl,

After putting in the snippet of code I receive the following error: "Object
reference not set to an instance of an object."

Line 63: // Obtain current user's shopping cart ID
Line 64: String cartId = cart.GetShoppingCartId();
Line 65:
Line 66: // Add Product Item to Cart
Line 67: int intProductID =
Convert.ToInt32(dlstProducts.DataKeys[e.Item.ItemIndex]);


Source File: c:\inetpub\wwwroot\psyche\boutique\default.aspx.cs Line: 65

I'm stumped why the Convert statement isn't resolving this error.

Andy
 
K

Karl Seguin

A little hard to troubleshoot since line 65 appears blank :)

there are only 4 possibilities:
cart is null
dlstProducts is null
e is null
e.Item is null

it's imossible for me to tell you which it is with what you've given
me...try stepping through your code and put a watch on each of those
items...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Andy Sutorius said:
Karl,

After putting in the snippet of code I receive the following error: "Object
reference not set to an instance of an object."

Line 63: // Obtain current user's shopping cart ID
Line 64: String cartId = cart.GetShoppingCartId();
Line 65:
Line 66: // Add Product Item to Cart
Line 67: int intProductID =
Convert.ToInt32(dlstProducts.DataKeys[e.Item.ItemIndex]);


Source File: c:\inetpub\wwwroot\psyche\boutique\default.aspx.cs Line: 65

I'm stumped why the Convert statement isn't resolving this error.

Andy



Karl Seguin said:
use e.Item.ItemIndex to get the index of the item

dlstProducts.DataKeys[e.Item.ItemIndex]

also, any particular reason you are using ToInt16 instead of 32?

Karl
 
A

Andy Sutorius

Karl,

After a good nights sleep I stepped through and it worked perfectly. I must
have forgotten to rebuild or something. Sorry to lead you on a wild goose
chase.

Thanks for your help!

Andy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top