none
System.NullReferenceException: Object reference not set to an instance of an object. RRS feed

  • Question

  • When using the Is Nothing command below I get the error seen below.

    System.NullReferenceException: Object reference not set to an instance of an object.

    I would appreciate any suggestions. Basically when the MapPhoto label is empty I would like the hyperlink to be invisible.

     

    <asp:TemplateField HeaderText="Map Photo" HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle" ItemStyle-Width="10%">
                    <ItemTemplate>
                        <p style="margin- padding: 0px 10px 0px 5px;">
                            <asp:Label ID="MapPhoto" runat="server" Text='<%# Eval("PHOTO2") %>'></asp:Label>
                            <asp:HyperLink class="popup" runat="server" ID="ViewPhoto2" NavigateUrl="#" Style="text-decoration: underline;">View Photo 2<span><img src='/user_images/<%# Eval("PHOTO2")%>'></span></asp:HyperLink>
                        </p>
                    </ItemTemplate>
                </asp:TemplateField>

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
            Dim MapPhoto As Label = DirectCast(e.Row.FindControl("MapPhoto"), Label)
            Dim ViewPhoto2 As HyperLink = DirectCast(e.Row.FindControl("ViewPhoto2"), HyperLink)
    
            If MapPhoto.Text Is Nothing Then
    
               ViewPhoto2.Visible = False
             
            End If
        End Sub
    

     
    Tuesday, April 10, 2018 3:16 PM

All replies

  • The presentation part is not done here. 

    It is done in the forum


    http://forums.asp.net

    Mostly these kind are problems are caused because ASP.Net has no state and at post back everything is nothing.

    In this case MapPhoto (the text can only be empty)


    Success
    Cor

    Tuesday, April 10, 2018 3:26 PM
  • You explained why I am having the problem but I don't really see a resolution. Can you help of not?
    Wednesday, April 11, 2018 2:45 PM
  • Never mind the last reply as I found another way to do this through the asp:Templatefield as seen below through "Visible" property of the asp:Hyperlink:

     
    <asp:TemplateField HeaderText="Map Photo" HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle" ItemStyle-Width="10%">
                    <ItemTemplate>
                        <p style="margin-top: -10px; padding: 0px 10px 0px 5px;">
                            <asp:Label ID="MapPhoto" runat="server" Text='<%# Eval("PHOTO2") %>'></asp:Label>
                            <asp:HyperLink ID="ViewPhoto2" class="popup" href="#" Style="text-decoration: underline;" runat="server" Visible='<%# Not String.IsNullOrEmpty(Convert.ToString(Eval("PHOTO2"))) %>'>View Photo 2<span><img src='/user_images/<%# Eval("PHOTO2")%>'></span></asp:HyperLink>
                        </p>
                    </ItemTemplate>
                </asp:TemplateField>

    Wednesday, April 11, 2018 3:35 PM