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

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"


cmd.Parameters.AddWithValue("@RecordID", gvr.Cells("RecordID").Value)
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

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?
glorrAuthor 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.

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
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
.NET Programming

From novice to tech pro — start learning today.