Solved

Exception for Datalist inside Formview when Datalist has no items.

Posted on 2007-04-03
3
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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