Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
|
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 November 28th, 2009, 07:58 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default Differences between LinqDS and sqlDs with setting AppendDAtaBoundItems="true" on ddl

Hello Imar,
I hope you can have a look at this question that has puzzled me a bit...quite a bit:
Take try it out on p 397 where we set up the filter using a ddl attached to an sqlDS, to show only records of a certain Genre in the Gridview. Of the ddl we set the AppendDataBoundItems=”true”, and we add a ListItem with value=””.
Now take try it out on p466. Again we use a ddl as a filter to show only certain records in the ListView. This time the ddl is attached to a LinqDS and we do not set any ListItem to initiate the ddl with a neutral value like for example “select a photo album”.
So, ok I tried to do exactly that and I found out setting AppendDataBoundItems=”true” doesn’t work, not even with value=”” the error you get is:


Operator '==' incompatible with operand types 'Int32' and 'Object'

The solution seems to be to set the DefaultValue=”0” on the ControlParamter of the LinqDS.
However this time you get the error:


Input string was not in a correct format.
This points to the the following code in the code behind:

int photoAlbumId = Convert.ToInt32(ddlPhotoAlbum.SelectedValue);

It seems that the DataBound event of the ListView that was added on p580 rejects the empty string photoAlbumOwner. Not very surprising,because:



This is actually the same story as we encountered on p 456 where you don’t want to open ManagePhotoAlbum.aspx without feeding it first the PhotoAlbumId querystring as a filter, also this does not work without a valid value in the string photoAlbumOwner. And here we set the DefaultValue=”-1” on the QueryStringParameter

So my question is, what is so different about the LinqDS compared to the sqlDs that we cannot set the AppendDataBoundItems =”true” on the ddl? And how can we initiate this ddl with a neutral value, without causing problems?

Thanks, Robin
 
Old November 28th, 2009, 08:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Robin,

I am not exactly sure what you're asking. AppendDataBoundItems is not related to the actual queries or data source controls. What AppendDataBoundItems does is keep items you define in markup view when items from a data source are added. So, when you have a "Please make a selection" item, it's preserved when you then add items from a data source.

The errors you may get are probably related to the value sent to the query based on the item in the drop down. Imagine an item like this:

Code:
 
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
  <asp:ListItem Value="Select Something">Please make a selection</asp:ListItem>
</asp:DropDownList>
Now, consider what happens when you choose this item.
Code:
 
int photoAlbumId = Convert.ToInt32(ddlPhotoAlbum.SelectedValue);
This will crash as you cannot convert the string "Select Something" to an integer.

You could assign a non existing integer to the ListItem:
Code:
 
<asp:ListItem Value="-1">Please make a selection</asp:ListItem>
or you can assign an empty string:

Code:
 
<asp:ListItem Value="">Please make a selection</asp:ListItem>
and then skip the code when the item is empty:

Code:
 
if (!string.IsNullOrEmpty(ddlPhotoAlbum.SelectedValue))
{
  int photoAlbumId = Convert.ToInt32(ddlPhotoAlbum.SelectedValue);
  ... Remaining code here
}
Does this help?

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 (November 28th, 2009)
 
Old November 28th, 2009, 09:55 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default

Hi Imar,

And thanks, it did help.
I understood what the AppendDataBoundItems does, but as a consequence you have to set the value of the <ListItem> to a certain value, like "" or "-1", that seemed to mess up things for me because both values are invalid for the photoAlbumId.

So what I missed was the IsNullOrEmpty() method, to check for that.

And so in the end the difference was not between the sqlDS and the LinqDs, this could have happened also in the DataBound event handler of the sqlDS I assume.

Sometimes it is a bit hard to find out where the errors originate if you don't know how to read the stacktrace, and then overview is easily lost...

Best regards, Robin






Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
<deployment retail="true"> clarification jsymons BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 October 1st, 2009 02:48 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





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