Question GridView RowUpdating issue

lkrterp

Member
Joined
Jul 10, 2007
Messages
21
Programming Experience
Beginner
Need help understanding GridView and RowUpdating button. I am trying to update a SQL database table using a GridView and the Edit and Update buttons. My issue is that I am confused on how to pull the values from the row edited into my SQL statement.

So far I have the following code for Updating:

VB.NET:
Protected Sub ResultsView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles ResultsView.RowUpdating

        Dim sUserName As String = ResultsView.SelectedRow.Cells(1).Text
        Dim sCorp As String = ResultsView.SelectedRow.Cells(2).Text
        Dim sDashIn As String = ResultsView.SelectedRow.Cells(3).Text
        Dim sDMO As String = ResultsView.SelectedRow.Cells(4).Text
        Dim sSMO As String = ResultsView.SelectedRow.Cells(5).Text
        Dim sSMOMF As String = ResultsView.SelectedRow.Cells(6).Text
        Dim sMaint As String = ResultsView.SelectedRow.Cells(7).Text
        Dim DB As ADODB.Connection
        Dim sSQL As String

        DB = New ADODB.Connection
        DB.Open("DSN=Warehouse", "xyz", "test")

        sSQL = "UPDATE sales_users SET corporate = '" & sCorp & "', "
        sSQL += "dashin = '" & sDashIn & "', "
        sSQL += "dmo = '" & sDMO & "', "
        sSQL += "smo = '" & sSMO & "', "
        sSQL += "smomf = '" & sSMOMF & "', "
        sSQL += "maint = '" & sMaint & "' "
        sSQL += "WHERE username = '" & sUserName & "'"

        DB.Execute(sSQL)

    End Sub


I keep getting an 'Object reference not set to an object instance error'. So I assume I am forgetting to do something in my code or I am completely wrong in what I have written. This is the first time I have tried using the Edit and Update buttons.

Any help would greatly be appreciated before I pull all my hair out. :confused:

Thanks
 
The "Object reference not set to an object instance error" is caused due the statement

Dim sUserName As String = ResultsView.SelectedRow.Cells(1).Text

The Gridview is rendered as a series of TR's and TD's and so searching for cell(1) doesn't necessarily return the textfield.
The best way to achieve what you are trying would be to do the following.
If the textbox name is txtUserName, then

Dim sUserName As String = ctype(ResultsView.SelectedRow.FindControl("txtUserName"), TextBox).Text

Similarily for the other fields as well..:)
 

Latest posts

Back
Top