?
Solved

Exception for Datalist inside Formview when Datalist has no items.

Posted on 2007-04-03
3
Medium Priority
?
499 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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