473,467 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

datakeys error

I have a datagrid and want to identify the primary key. Each time I try I
get an error

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name: index

The code I am using seems straight forward enough.

Private Sub DG_Glossary_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DG_Glossary.SelectedIndexChanged

Dim IDIndex As Integer
Dim IDKey As Integer
IDIndex = DG_Glossary.SelectedIndex
IDKey = DG_Glossary.DataKeys(IDIndex)
.............
end sub

IDIndex calculates fine but IDKey causes an error.
Nov 19 '05 #1
2 896
I'm not quite sure, but it might be that IDIndex is -1 if nothing has been
selected yet.
Put an if test that checs if IDIndex is >= 0 before you get the key.

HTH,
Lars-Erik

"NevilleT" <Ne******@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
I have a datagrid and want to identify the primary key. Each time I try I
get an error

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index

The code I am using seems straight forward enough.

Private Sub DG_Glossary_SelectedIndexChanged(ByVal sender As
System.Object,
ByVal e As System.EventArgs) Handles DG_Glossary.SelectedIndexChanged

Dim IDIndex As Integer
Dim IDKey As Integer
IDIndex = DG_Glossary.SelectedIndex
IDKey = DG_Glossary.DataKeys(IDIndex)
............
end sub

IDIndex calculates fine but IDKey causes an error.

Nov 19 '05 #2
Hi Lars
Thanks for the tip but IDIndex is fine. I am using VS2003 and checked by
stepping through the code. There is something else I am missing.
"Lars-Erik Aabech" wrote:
I'm not quite sure, but it might be that IDIndex is -1 if nothing has been
selected yet.
Put an if test that checs if IDIndex is >= 0 before you get the key.

HTH,
Lars-Erik

"NevilleT" <Ne******@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
I have a datagrid and want to identify the primary key. Each time I try I
get an error

System.ArgumentOutOfRangeException: Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index

The code I am using seems straight forward enough.

Private Sub DG_Glossary_SelectedIndexChanged(ByVal sender As
System.Object,
ByVal e As System.EventArgs) Handles DG_Glossary.SelectedIndexChanged

Dim IDIndex As Integer
Dim IDKey As Integer
IDIndex = DG_Glossary.SelectedIndex
IDKey = DG_Glossary.DataKeys(IDIndex)
............
end sub

IDIndex calculates fine but IDKey causes an error.


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marina | last post by:
Hi, There is a component that is binding the results of a search to a DataList. By clicking on an image in the datalist, the user can download that particular item. We are using the DataKeys...
2
by: Dave | last post by:
Can anybody help me with BaseDataList.DataKeys? I always get this error: Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size...
0
by: Lingo | last post by:
I'm using beta2. I'm trying to create a gridview dynamically with a passed in SQL statement, everything works paging, sorting, and adding columns dynamically with the code. Because the hidden...
2
by: Narshe | last post by:
I have a gridview with datakeyname="Id" that is using a sqldatasource. When I click delete on the gridview, the datakeys property is empty during the deleting event.
1
by: | last post by:
I got stuck today on an annoying problem involving DataKeys, datasources, and the GridView: I kept getting errors saying things to the effect of""Item has already been added. Key in dictionary:...
6
by: Greg | last post by:
Hello, I have a GridView bound to a custom object. I set the DataKeyNames property along with the column DataField properties at design time, and bind the GridView to my object at run-time. In...
1
by: teju | last post by:
Hello, I have two datakeys defined in the grid view. I want to access the both the datakeys when u click a button. Iam able to access one of the datakeys but not the other. Below is the code ...
0
by: =?Utf-8?B?cmxt?= | last post by:
I have a gridview updating every 5 seconds. There are several DataKeys declared which I access successfully in the RowDataBound event. However, in the ROwCommand event the count of the DataKeys...
2
by: Vajrala Narendra | last post by:
Hi, all am working on asp.net, c# 2.0 i want to delete a row from a grid view for that i take datakeyname inn gris view i wrote coding as <asp:GridView ID="GridView2" runat="server"...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.