how to remove System.NullReferenceException?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ishanisircar
    New Member
    • Feb 2009
    • 3

    how to remove System.NullReferenceException?

    hiya,

    i am using vb and asp.net with sql server. when the vb page is run, a gridview is displayed. In the gridview when a link button is clicked, a calendar is displayed. i am using this code to displat the calendar. but i keep getting a System.NullRefe renceException. how can i remove this?

    i get an error on this line of my code

    Code:
    CType(grTemp.FindControl("txtRenewTill"), TextBox).Visible = True
    here is my code:

    Code:
    Protected Sub GridViewRenew_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridViewRenew.SelectedIndexChanged 
    Dim grTemp As GridViewRow
    
    Dim selected_date As DateTime 
    selected_date = CalendarRenew.SelectedDate
    
    For Each grTemp In GridViewRenew.Rows 
    grTemp = GridViewRenew.SelectedRow
    
    CalendarRenew.Visible = True
    
    CType(grTemp.FindControl("txtRenewTill"), TextBox).Visible = True
    
    Next
    
    end sub 
    
    Protected Sub CalendarRenew_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalendarRenew.SelectionChanged 
    Dim grTemp As GridViewRow
    
    For Each grTemp In GridViewRenew.Rows 
    grTemp = GridViewRenew.SelectedRow
    
    CType(grTemp.FindControl("txtRenewTill"), TextBox).Text = CalendarRenew.SelectedDate 
    Next
    
    End Sub
    ------------------------

    thanks a lot.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    grTemp is not finding any control called "txtRenewTi ll" as a child control of itself. When FindControl failes it returns null. You should be checking for this.

    Comment

    • shweta123
      Recognized Expert Contributor
      • Nov 2006
      • 692

      #3
      Hi,

      You can do the following modification in the code in order to remove the Null Reference error:
      Code:
       Dim txtTemp as new Textbox()
      txtTemp =  CType(grTemp.FindControl("txtRenewTill"), TextBox)
      If(IsdbNull(txtTemp)= False then 
          CType(grTemp.FindControl("txtRenewTill"), TextBox).Visible = True
      End If
      Last edited by Frinavale; Feb 2 '09, 04:13 PM. Reason: added [code] tags

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by shweta123
        Hi,

        You can do the following modification in the code in order to remove the Null Reference error:
        Or you could just check it like so:

        Code:
        Dim txtTemp As TextBox = CType(grTemp.FindControl("txtRenewTill"), TextBox)
        
        If txtTemp IsNot Nothing then 
           txtTemp.Visible = True
        End If

        I have a feeling that you're getting the Null Reference error because your TextBox ("txtRenewTill" ) is not in the template that the row is using.

        Do you have a TextBox named "txtRenewTi ll" in your GridView's Edit Template?

        If so, then you can only access it if the Edit Template is being used and you'll have to change your code to display the calendar when Editing a row instead of when a row is Selected.

        -Frinny

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I don't think isdbNull() would work in this case since it would be a real null (Nothing) and not a database null?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Plater
            I don't think isdbNull() would work in this case since it would be a real null (Nothing) and not a database null?
            Those were my thoughts too...I wasn't sure and that's why I recommending checking for "Nothing" instead of DBNull.

            Comment

            Working...