Trouble getting multiple data key values

I have a gridview with multiple datakeys values but I does not seem to grab them correctly.

How can at multiple data keys values

When I try this.
Dim Id As Integer = myGridviewIN.DataKeys(myGridviewIN.SelectedIndex).Item(0)
 Dim SSN As Integer = myGridviewIN.DataKeys(myGridviewIN.SelectedIndex).Item(1)
I get 
Index was out of range. Must be non-negative and less than the size of the collection.

When i try this.
 Dim Id As Integer = myGridviewIN.DataKeys(myGridviewIN.SelectedIndex).Values("intScheduleId")

I get the same error as above but on postback of the link button the commandname is attached to instaed of page load.




 If (e.CommandName = "CheckIn") Then
            Dim Id As Integer = myGridviewIN.DataKeys(myGridviewIN.SelectedIndex).Values("intScheduleId")
            Dim SSN As Integer = myGridviewIN.DataKeys(myGridviewIN.SelectedIndex).Values("strSSN")

            sql = "Insert tblSRPAttendance (intScheduleId, strSSN, dtTimeIn, bitActive, strLogged) VALUES (" & Id & ", '" & SSN & "', '" & Date.Now & "', " & 1 & ", '" & Userlogon & "')"

            Try
                insertUpdateDelete(sql)
            Catch ex As Exception
                lblError.Text = ex.Message.ToString & " - ERROR Save Personnel   <br> " & lblErrorMsg.Text
                SetFocus(aError)
            End Try

            FillGridview()
        End If

Open in new window

kdeutschAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
make sure you have
<asp:GridView ID="myGridviewIN" runat="server" DataKeyNames="intScheduleId, strSSN">
kdeutschAuthor Commented:
IJZ:

I have that in my Gridview HTML.
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,
Please use following code

Protected Sub myGridviewIN_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles myGridviewIN.RowCommand
        Dim row As GridViewRow = CType(e.CommandSource, GridViewRow)
        If (e.CommandName = "CheckIn") Then
            Dim Id As Integer = myGridviewIN.DataKeys(row.RowIndex).Values("intScheduleId")
            Dim SSN As Integer = myGridviewIN.DataKeys(row.RowIndex).Values("strSSN")

            Sql = "Insert tblSRPAttendance (intScheduleId, strSSN, dtTimeIn, bitActive, strLogged) VALUES (" & Id & ", '" & SSN & "', '" & Date.Now & "', " & 1 & ", '" & Userlogon & "')"

            Try
                insertUpdateDelete(Sql)
            Catch ex As Exception
                lblError.Text = ex.Message.ToString & " - ERROR Save Personnel   <br> " & lblErrorMsg.Text
                SetFocus(aError)
            End Try

            FillGridview()
        End If
    End Sub
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

kdeutschAuthor Commented:
IJZ

i get the following error on page load.

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.GridViewRow'.

It calls out the following line.
  Dim row As GridViewRow = CType(e.CommandSource, GridViewRow)
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
on place of Dim row As GridViewRow = CType(e.CommandSource, GridViewRow)
use following

if your command is button then please use following
Dim objTempButton As Button = DirectCast(e.CommandSource, Button)
Dim row As GridViewRow = DirectCast(objTempButton.NamingContainer, GridViewRow)


if your command is linkbutton then please use following
Dim objTempButton As LinkButton = DirectCast(e.CommandSource, LinkButton)
Dim row As GridViewRow = DirectCast(objTempButton.NamingContainer, GridViewRow)


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
kdeutschAuthor Commented:
thanks
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.NET

From novice to tech pro — start learning today.