GridView Returning record id to update database

I have a selection criteria that pulls a selection of records (as few as 2, as many as 98)associated with a user and displays them in a GridView. There is also a drop down list that is in the GridView, which is used to make a selection. Then the user clicks on the Save button, with the intent of returning back their selections to the data base.

The issue I am having oddly enough isn't with the embedded grid view drop down, or even getting it to write to the data base. The issue I am having is writing to the correct location as I'm failing to properly grab the recordID that the drop down selection should be writing to. When the user selects to update, it will be to multiple records.

recordID (needs to be read to direct the recordDropDown selected value to the proper record)
recordDetail (does not get returned, data contained is used in the decision process for selecting the recordDropDown value)
recordDropDown (needs to be written to the appropriate recordID row in the database)

Your help is greatly appreciated.
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Dim cmd As New SqlCommand
            cmd.Connection = cn

            Dim gvr As GridViewRow
            Dim ddl As New DropDownList
            For Each gvr In gvSelection.Rows
                If gvr.RowType = DataControlRowType.DataRow Then
                    ddl = CType(gvr.FindControl("gvddl"), DropDownList)
                    cmd.CommandText = "spUPDRecord"
                    cmd.CommandType = CommandType.StoredProcedure
                    'Everything Ive used here so far in the XXX has failed, this is where Im trying to pass the RecordID back to ensure the correct record is updated.'
                    cmd.Parameters.AddWithValue("@RecordID", XXX)
                    cmd.Parameters.AddWithValue("@DropDownSelection", ddl.SelectedValue)
                End If

            lblSuccess.Visible = True
            lblSuccess.Text = "You have successfully added the selections to the database"

        Catch ex As Exception
            lblerror.Visible = True
            lblerror.ForeColor = System.Drawing.Color.Red
            lblerror.Text = "An error has occurred.  Please contact the webmaster and provide the error message listed below:<br /><br />" & ex.Message
            If Not cn Is Nothing Then cn.Close()
        End Try

    End Sub

Open in new window

Who is Participating?
glorrConnect With a Mentor Author Commented:
Setting the gvr to a different row did not help. I've adjusted course since I'm beginning to believe I was taking the wrong approach. I've altered the RecordID field to a template field and now when it is getting populated it is writing to a Label control.

This allows me to locate it via FindControl, convert it to an int, and successfully update the proper records now.

It may be a different solution than what you were helping me drive towards, but it was your help and recommendations that steered me towards the final solution.

I appreciate the time you took to help me out.
CodeCruiserConnect With a Mentor Commented:
Should you not be using

glorrAuthor Commented:
Do you mean the following?

cmd.Parameters.AddWithValue("@RecordID", gvr("RecordID"))

If so it tells me that it doesn't like "gvr as it cannot be indexed because it has no default property"

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

CodeCruiserConnect With a Mentor Commented:

cmd.Parameters.AddWithValue("@RecordID", gvr.Cells("RecordID").Value)
glorrAuthor Commented:
.Value is not a member of Cells

I tried going with just gvr.Cells("RecordID")  and I received Conversion from string "PPAID" to type 'Integer' is not valid.

So assuming that it is grabbing the actual value, then I might just need to figure out how to get a good Convert.ToInt32 statement going. My first couple of attempts I've just tried haven't been fruitful.
No converter would convert the PPAID to int. Did you try .Text property. What value are you expecting?
glorrAuthor Commented:
The recordID in the data base is an integer, so I'm hoping to pass on back to it.

Ah, and the PPAID is my "RecordID" alias. RecordID is just easier to explain then PPAID.

So that error message should be "Conversion from string "RecordID" to type 'Integer' is not valid."
Is PPaid the column header?
glorrAuthor Commented:
Yes. It is the column header.
Then you are in the wrong row. If you ignore this row and move on to next row, do you get int values?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.