Link to home
Start Free TrialLog in
Avatar of glorr
glorrFlag for United States of America

asked on

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
        Try
            cn.Open()
            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)
                    cmd.ExecuteNonQuery()
                    cmd.Parameters.Clear()
                End If
            Next

            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
        Finally
            If Not cn Is Nothing Then cn.Close()
        End Try

    End Sub

Open in new window

SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of glorr

ASKER

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"

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of glorr

ASKER

.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?
Avatar of glorr

ASKER

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?
Avatar of glorr

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial