Solved

Exception for Datalist inside Formview when Datalist has no items.

Posted on 2007-04-03
3
494 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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