• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 848
  • Last Modified:

GridView DataKeyNames Out of Range Error - VB.NET/ASP.NET

Every time i click Select on one of the grid view's record, i get an exception at this snippet:

GridView1.DataKeyNames.SetValue(" Any ID", 0)

What can i do?
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        If ViewState("currentGridView") = "Jobs" Then
            GridView1.DataKeyNames.SetValue("job_id", 0)
            Session("PK") = Convert.ToString(GridView1.SelectedValue)
            Response.Redirect("EditJobs.aspx")
        ElseIf ViewState("currentGridView") = "Publishers" Then
            GridView1.DataKeyNames.SetValue("pub_id", 0)
            Session("PK") = Convert.ToString(GridView1.SelectedValue)
            Response.Redirect("EditPublishers.aspx")
        Else
            GridView1.DataKeyNames.SetValue("stor_id", 0)
            Session("PK") = Convert.ToString(GridView1.SelectedValue)
            Response.Redirect("EditStores.aspx")
        End If
    End Sub

Open in new window

0
F-J-K
Asked:
F-J-K
  • 4
  • 2
2 Solutions
 
HainKurtSr. System AnalystCommented:
GridView1.DataKeyNames.SetValue("job_id", 0)
Session("PK") = Convert.ToString(GridView1.SelectedValue)
Response.Redirect("EditJobs.aspx")

after doing something here, you are redirecting... which makes previous statements meaningless...

also instead of Redirect use Server.Transfer("EditJobs.aspx", true)
0
 
HainKurtSr. System AnalystCommented:
also did you set datakeynames in aspx?

<asp:GridView ID="GridView1" DataKeyNames="job_id,pub_id,stor_id"...
0
 
F-J-KAuthor Commented:
In EditJobs.aspx page, i take appropriate actions based on PK session value. I will try your second suggestion and let you know how its going on...
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
F-J-KAuthor Commented:
Let me clarify the error as is:

-----------------------------

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

Line 41:             GridView1.DataKeyNames(0) = "job_id"
Line 42:             Session("PK") = Convert.ToString(GridView1.SelectedValue)

-------------------------------------------

After i click search button, i get the gridview. When i select a row, i get the error you see above.
The above snippet (line 41 and 42) resides in GridView1_SelectedIndexChanged event.

This is how GridView control in my aspx page looks like:

<asp:GridView ID="GridView1" runat="server">

Note: putting DataKeyNames="job_id,pub_id,stor_id" in the GridView control doesn't help. It only works if target a single DataKeyName only such as DataKeyNames="job_id".

What should i do?
Untitled.jpg
0
 
F-J-KAuthor Commented:
Thanks
0
 
F-J-KAuthor Commented:
Discard my last questions...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now