Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 26th, 2008, 09:46 AM
Registered User
 
Join Date: Jun 2008
Location: Crewe, Cheshire, United Kingdom.
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......
Reply With Quote
  #2 (permalink)  
Old June 26th, 2008, 10:48 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
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
================================================== =========
Reply With Quote
  #3 (permalink)  
Old June 26th, 2008, 01:57 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #4 (permalink)  
Old June 26th, 2008, 04:15 PM
Registered User
 
Join Date: Jun 2008
Location: Crewe, Cheshire, United Kingdom.
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......
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.