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

LVL 1
F-J-KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.