Object Reference not set to an instance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krisssgopi
    New Member
    • Apr 2010
    • 39

    Object Reference not set to an instance

    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.

    Code:
    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
  • xMetalDetectorx
    New Member
    • Oct 2008
    • 44

    #2
    Which line did it specifically throw the error on? Please debug line by line if you can and let us know. Thanks.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      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 [il]Nothing[/il]. 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:
      Code:
      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)

      Comment

      • krisssgopi
        New Member
        • Apr 2010
        • 39

        #4
        Originally posted by Frinavale
        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 [il]Nothing[/il]. 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:
        Code:
        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)
        Thank a lot for your reply. Its working fine now.

        Comment

        Working...