Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 26th, 2008, 09:46 AM
Registered User
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object variable or With block variable not set

Hi Guys

I'm new here so good day to you all.

I've been staring at and Googling this one for hours without success, so any help would be greatly appreciated.

I have an aspx page containing a repeater.

I am using the ItemCreated event of the Repeater to either display a asp:Button or asp:Label depending on the contents of one of the fields returned to the repeater by the SQLDataSource.

So far all is good as the data is interogated correctly and either button or label displayed, however, since adding this event to my code behind file i find the error "Object variable or With block variable not set" is returned when I click any button or link on the page.

Code for the event:

Protected Sub Repeater1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim lbl As Label = CType(e.Item.FindControl("lblDelivered"), Label)
            Dim btn As Button = CType(e.Item.FindControl("btnDeliver"), Button)
            If e.Item.DataItem("Delivered") = True Then
                lbl.Visible = True
                btn.Visible = False
            Else
                lbl.Visible = False
                btn.Visible = True
            End If
        End If
    End Sub

Does anyone have any ideas as to why this error is returned and how to avoid it?

Thanks in advance

IC

Just de-bugging now boss......
 
Old June 26th, 2008, 10:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there...

This is a common error. It means that an object is nothing when you are trying to do something with it... can you trace to the line that is failing??? maybe the find control are not finding the control you are looking for? Since you are either displaying a label or a button, are you sure that the other control is rendering to the page?

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old June 26th, 2008, 01:57 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I suspect the problem lies in this line:

     If e.Item.DataItem("Delivered") = True

The default behavior of repeating data bound controls (repeater, datagrid, gridview, etc) is such that when you post back a page, the control is reconstructed from viewstate. The control's data source is null and thus each item's DataItem is null. All the items of the control are reconstructed purely from viewstate so you are accessing a property (DataItem) that is null on postback.

One option may be to change your code to run in the ItemDataBound event instead of ItemCreated. ItemCreated runs every time a repeating item is created, on data binding or on postback reconstruction. ItemDataBound, as the name suggests, only runs when the item is data bound. When the item is reconstructed it should be done so with the state it was in when rendered so you shouldn't need to even have that code run during a post back anyway. It should need only to run when you do the initial data bind. The visibility of your two controls should be properly restored without the need to set them explicitly.

-Peter
compiledthoughts.com
 
Old June 26th, 2008, 04:15 PM
Registered User
 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

Thank you for your response. I changed the event to the DataBound event and the error no longer occurs

Many thanks again

Ian

Just de-bugging now boss......





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable or With block variable not set haidee_mccaffrey Classic ASP Professional 5 March 8th, 2007 03:34 PM
"object variable or with block variable not set" netfresher ASP.NET 1.0 and 1.1 Basics 1 June 12th, 2006 03:50 PM
object variable or with block variable not set Aoude BOOK: Beginning VB.NET Databases 1 February 24th, 2006 05:21 PM
Object variable or With block variable not set tparrish VB Databases Basics 1 May 25th, 2005 10:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.