Solved

Exception for Datalist inside Formview when Datalist has no items.

Posted on 2007-04-03
3
440 Views
Last Modified: 2013-11-07
I keep getting this error even though my web app seems to run properly:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object.

This error is produced by this code:

    Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated
        Dim dl As DataList = FormView1.FindControl("DataList1")
        AddHandler dl.ItemCommand, AddressOf DataList1_ItemCommand
    End Sub

Basically, I have a datalist inside a formview. The datalist loads a bunch of thumbnails, so I added an event handler so that the image shows (in the current page) whenever someone clicks on a thumbnail.

However, I think this error is being produced when the Datalist has no items. I'm not really sure. Any IDeas?
0
Comment
Question by:Skytide
3 Comments
 
LVL 6

Expert Comment

by:riyazthad
Comment Utility
What you are doing DataList1_ItemCommand method? Can you paste code?
0
 

Author Comment

by:Skytide
Comment Utility
   Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
        If e.CommandName = "ChangePhoto" Then
            Dim dl As DataList = FormView1.FindControl("DataList1")
            Dim myPhoto As Image = FormView1.FindControl("imgPhoto")
            Dim myPhotoThumb As Image = CType(e.Item.FindControl("ibThumb"), Image)

            Dim myNewPhotoURL As String = myPhotoThumb.ImageUrl.Replace("_s.jpg", ".jpg")
            myPhoto.ImageUrl = myNewPhotoURL
        End If
    End Sub
0
 
LVL 5

Accepted Solution

by:
mirmansoor earned 500 total points
Comment Utility
Well.. If this error really occurs only when there are no items in the datalist. Then therez no point adding the event handler to it when the datalist is not there at all. u r getting that error because the FindControl("DataList1") is returning null. that means the datalist is not rendered since it doesnt have any items.. so you could rewrite the code like..
Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated
        Dim dl As DataList = FormView1.FindControl("DataList1")
        If Not dl Is Nothing Then
            AddHandler dl.ItemCommand, AddressOf DataList1_ItemCommand
       End If
    End Sub
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now