Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 January 18th, 2010, 11:00 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default unexpected behaviour of LinqDataSource_Updated

Hi there,

I tried to adapt the try it out on p444, in step 11 where in the LinqDataSource_Inserted event handler a redirect is made based on a querystring taken from the result set in the following way:

Besides using the Inserted eventhandler I used the Updated eventhandler, in the same way, with the same code. In the Inserted this code works, in the Updated not. It throws an error : Object reference not set to an instance of an object., highlighting the line with "Accomodation"


Strangely enough if I hit the back button and try it again, it will work.
The code is
Code:
protected void LqDSToi_Updated(object sender, LinqDataSourceStatusEventArgs e)
    {
        if (e.Result != null)
        {
            ThingsOfInterest myToi = (ThingsOfInterest)e.Result;
            if (myToi.ToiType.Name != "Accomodation")
            {
                Response.Redirect(string.Format("ManagePhotos.aspx?Id={0}&Type=Toi", myToi.ToiId.ToString()));
            }
            else
            {
                Response.Redirect(string.Format("AddEditAccomodation.aspx?ToiId={0}", myToi.ToiId.ToString()));
            }
        }
        else
        {
            Response.Redirect("Toi.aspx");
        }

    }
Any suggestions?
Best regards, Robin
 
Old January 18th, 2010, 11:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Maybe the error is thrown in ManagePhotos.aspx that tries to access an album with a Toi ID?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 18th, 2010, 06:01 PM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default Thanks for the suggestion Imar, but that's not it..

After testing my code with different variations of the condition that I use and the redirect, I have come to the conclusion that something else must be wrong...

A code like below for example works, whether "Henkie" exists or not, it just redirects to either one of the 2 possibilities and also the updates (and inserts) work, no prob

Code:
protected void LqDSToi_Updated(object sender, LinqDataSourceStatusEventArgs e)
    {
        if (e.Result != null)
        {
            ThingsOfInterest myToi = (ThingsOfInterest)e.Result;
            if (myToi.ToiName == "Henkie")
            {
                Response.Redirect(string.Format("AddEditAccomodation.aspx?ToiId={0}", myToi.ToiId.ToString()));
            }
            else
            {
                Response.Redirect(string.Format("ManagePhotos.aspx?Id={0}&Type=Toi", myToi.ToiId.ToString()));
            }
        }
        else
        {
            Response.Redirect("Toi.aspx");
        }

    }
What has changed, is that in the condition I now use a direct property of my ThingsOfInterest class ; ToiName (which results in nonsense, just for the sake of testing however...) and in the previous post I used a property of the ToiType class which in itself is a property of the ThingsOfInterest class (intellisense shows these properties).
In the LinqDataSource_Inserted, this works! In the LinqDataSource_Updated not... it does not seem to be accessible in the Result.

The TypeId is fed into the ThingsOfInterest through a dropdownlist, like so

Code:
<asp:TemplateField HeaderText="Type" SortExpression="TypeId">
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlType" runat="server"
                        DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="TypeId" 
                        SelectedValue='<%# Bind("TypeId") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="ddlType" runat="server" AppendDataBoundItems="True" 
                        DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="TypeId" 
                        SelectedValue='<%# Bind("TypeId") %>'>
                        <asp:ListItem Value="">select type of thing of interest</asp:ListItem>
                    </asp:DropDownList>
                    <br />
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("TypeId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
with the following sqlDataSource ( I had doubts whether the mistake could be here so I tried the same with a LinqDs for the ddl, without change)
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [TypeId], [Name] FROM [ToiType]"></asp:SqlDataSource>
I hope you can have a look at it and have something wise to say about it!

Best regards, Robin
 
Old January 18th, 2010, 06:12 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you sure it's Henkie, not Joop or Piet (those are test names we used at the office).... ;-)

Anyway, I think I may see what's going on as I recently ran into this with the EntityDataSource control as well: on updating, no round-trip to the database is made but the object to be updated is reconstructed from ViewState / Control State. So, unless you explicitly bind properties or add them to the DataKeyNames property of the data-bound control, the properties are all set to null in the Update event and you can only access the bound ones.

Try adding the ToiName property to the DataKeyNames of your list control and see if that helps.

To make discussions like this easier to follow and understand, can you clarify statements like: "it does not seem to be accessible in the Result"? Does it not show up in IntelliSense? Do you get an error when you try to access it? Does it contain null? And so on and so forth.... ;-)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 18th, 2010, 06:19 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW: if that doesn't help, try explaining how the TypeId is related to this; it doesn't seem to be used in the Code Behind....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 19th, 2010, 06:28 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default Sounds like that must be it.... any other suggestions?

Hi Imar,
Thanks for your answer, I tried what you suggested...



I tried to add the property that I want to use in the Updated eventhandler, the ToiType.Name to the DataKeyNames of the ddl (with the ToiType table as its datasource, this becomes DataKeyNames=”Name”).
With the same error as a result.


Then I tried adding the ToiType to the DataKeyNames of the DetailsView (the ddl is in a template of this detailsview) like so: DataKeyNames="ToiId, ToiType" ( ToiId is the PK of the ThingsOfInterest table which is the datasource of the DetailsView) with the following error :
Type 'ToiType' in Assembly 'App_Code.elgmanoy, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.


So to summarize: I can in the if statement use myToi.ToiName as a condition (ToiName is just a property of ThingsOfInterest) ; myToi.ToiName == “Joop” will evaluate to either true or false and no errors are thrown. This must mean that myToi is set to an object.
I can however in the if statement NOT use myToi.ToiType.Name, this will throw the error “Object reference not set to an instance of an object” and so it must be that ToiType is not set to an object.


ToiType, is my type table for the ThingsOfInterest, and it has a many to one relationship with the ThingsOfInterest table, the PK is ToiType and the FK ThingsOfInterest table is TypeId.


The ToiType is bound to its FK TypeId and comes from the ddl in the template, so how do I set it explicitly to an object in the update?


best regards, Robin




 
Old January 19th, 2010, 06:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, it looks like the complex property ToiType is not serialized; which makes sense as object graphs are not serialized....

One way to do it is to refetch the Toi from the database in the event handler and the update the properties you want to change.

Sorry I can't recommend much else. With all these Toi / ToiTypes / ThingsOfInterest you're not making it very easy to see the problem at hand, let alone reproduce it.... ;-)

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
robbaralla (January 19th, 2010)
 
Old January 19th, 2010, 06:42 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default

I appreciate your help Imar and I realize that I probably don't make it very clear so its a bit hard to catch the drift here. I'll have another look at it and see if I can use your next suggestion.
Thanks again for your time.

Best regards, Robin
 
Old January 19th, 2010, 07:06 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default

Ok, Imar, it works!
I followed your last suggestion; in the updated eventhandler I fetched the just inserted record again -through a simple Linq query- from the database and used in this way the corresponding property ToiType.Name to compare it in the if statement with "Accomodations".
Maybe this slows down the process, but ok it works.

regards, Robin
 
Old January 19th, 2010, 02:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Great; thanks for the update....

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Behaviour VeganMan BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 9 April 14th, 2008 11:16 AM
Strange behaviour with IE smolds BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 October 15th, 2005 08:49 PM
preceding-sibling behaviour andrewcday XSLT 6 December 3rd, 2004 03:24 PM





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