Grid View Row updating

Object Reference not set to an instance.

Page 1 of 1

1 Replies - 12740 Views - Last Post: 12 July 2010 - 09:00 AM Rate Topic: -----

#1 krisssgopi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 10

Grid View Row updating

Posted 09 July 2010 - 10:17 PM

Hi Team,

Am using Grid View in ASP.Net while am trying to update the row it was not getting updated. Instead, it throw an exception Object reference not set to an instance. please help me in this regard.

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Try
            Dim txtName, txtName1, txtName2, txtname3 As New TextBox
            Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
            txtName = CType(row.FindControl("txtName"), TextBox)
            txtName1 = CType(row.FindControl("txtName1"), TextBox)
            txtName2 = CType(row.FindControl("txtName2"), TextBox)
            txtname3 = CType(row.FindControl("txtName3"), TextBox)

            'doupdate(txtName1, txtName2, txtname3, txtName)
            'con.Open()
            cmd = New MySqlCommand("update login set username='" & txtName1.Text & "',password='" & txtName2.Text & "',Usertype='" & txtname3.Text & "' where id='" & txtName.Text & "')", con)
            da = New MySqlDataAdapter(cmd)
            cmd.ExecuteNonQuery()
            ds = New DataSet
            da.Fill(ds)
            GridView1.DataSource = ds
            GridView1.DataBind()
            con.Close()
        Catch ex As Exception
msgbox(ex.Message.ToString)
        End Try
        
        
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Grid View Row updating

#2 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Grid View Row updating

Posted 12 July 2010 - 09:00 AM

You are retrieving TextBoxes from the row using the FindControl command.
The FindControl method may not have been able to retrieve a TextBox though and so it will return Nothing. You should always check to make sure that the controls that you are retrieving exist before you attempt to use them.

For example, the following will check to make sure that "txtName" exists:
If txtName IsNot Nothing Then
  'txtName was successfully retrieved...it is not nothing
  'you can use it without experiencing a Null Reference Exception
Else
  'something went wrong and txtName does not exist 
End If


Are you sure that the IDs of the TextBoxes are correct?

-Frinny

(PS you should be using parameters to create your SQL command...you should not be dynamically creating your SQL statement by concatenating user provided data into your SQL command because this leaves you open to a SQL Injection Attack)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1