Solved

Exception for Datalist inside Formview when Datalist has no items.

Posted on 2007-04-03
3
466 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
ID: 18848266
What you are doing DataList1_ItemCommand method? Can you paste code?
0
 

Author Comment

by:Skytide
ID: 18848449
   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
ID: 18848779
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now