Link to home
Start Free TrialLog in
Avatar of luscoma
luscomaFlag for United States of America

asked on

ASP:Listview and LinqToSQLDataSource Error

Okay, I've had some strange issues using the asp:Listview control and the LinqtoSQL datasource control.

It is binding fine and will show up the items as expected; however, when I try to click an item it will never use the SelectedItemTemplate.  I did some digging and when debugging decided to set the selectedindex manually just to see if I could.  When I do it, it throws an exception saying an object reference not set to an instance of an object.  

Now I feel like this is due to my lack of experience using the declarative datasource objects (before I've done it manually) but I just can't seem to figure out why it won't select an item.

Judging by my stack trace (included) it seems that there may be an issue somewhere with the datasource's setup and perhaps my misunderstanding of the select setup.

I'm going to paste my listview and the datasource as well as the stack trace and would appreciate any help available!

PS: The table is fairly simple it just includes an Id (primary key), Name (varchar(40))
// ASP.Net Code
// -------------------------
<asp:LinqDataSource ID="LINQCategories" runat="server" 
                            ContextTypeName="AIMDB2.AIMdb" TableName="InventoryCats" Select="new (Id, Name)">
                        </asp:LinqDataSource>
 
// ListView
<asp:ListView ID="lvCategories" runat="server" GroupItemCount="6"
                                    DataSourceID="LINQCategories">
	                                    <LayoutTemplate>
		                                    <table id="Table1" runat="server">
			                                    <tr id="Tr1" runat="server">
				                                    <td id="Td1" runat="server">
					                                    <table ID="groupPlaceholderContainer" runat="server">
						                                    <tr runat="server" id="groupPlaceholder" />
					                                    </table>
				                                    </td>
			                                    </tr>
			                                    <tr id="Tr2" runat="server">
				                                    <td id="Td2" runat="server" style="">
				                                    </td>
			                                    </tr>
		                                    </table>
		                                    <asp:DataPager runat="server" ID="DataPager" PageSize="12">
                                                <Fields>
                                                  <asp:NumericPagerField runat="server" ButtonCount="10"
                                                       PreviousPageText="<--"
                                                       NextPageText="-->" />
                                                </Fields>
                                            </asp:DataPager>
	                                    </LayoutTemplate>
	                                    <GroupTemplate>
	                                        <tr runat="server" id="CatagoryItem">
	                                            <td runat="server">
	                                                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
	                                            </td>
	                                        </tr>
	                                    </GroupTemplate>
	                                    <ItemTemplate>
	                                        <td id="Td1" runat="server" style="font-family:Arial;padding: 5px 5px; font-size:small;" >
	                                            <asp:Image ID="Image1" Width="80" Height="80" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1" onerror="DisplayDefaultCatalogImage(this)"  ImageUrl='<%#Eval("Id","http://catalog.motionmedical.com/Itemcat/{0}.jpg") %>' style="width:100px"/><br />
	                                            <p style="vertical-align:bottom;"><%#Eval("Name") %></p>
	                                        </td>
	                                    </ItemTemplate>
	                                    <EmptyDataTemplate>
	                                        <td id="Td1" runat="server" style="font-family:Arial;font-size:0.5em;padding: 5px 5px;">
	                                            No Data Available
	                                        </td>
	                                    </EmptyDataTemplate>
	                                    <EmptyItemTemplate>
	                                        <td id="Td1" runat="server">&nbsp;</td>
	                                    </EmptyItemTemplate>
	                                    <SelectedItemTemplate>
	                                        <td id="Td1" runat="server">
	                                            HI
	                                        </td>
	                                    </SelectedItemTemplate>
                                    </asp:ListView>
 
//Stack Trace
//-------------------------
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
Stack Trace:
 
[NullReferenceException: Object reference not set to an instance of an object.]
   System.Data.Linq.SqlClient.QueryConverter.VisitInvocation(InvocationExpression invoke) +1503171
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1329
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +34
   System.Data.Linq.SqlClient.QueryConverter.VisitBinary(BinaryExpression b) +52
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +591
   System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +34
   System.Data.Linq.SqlClient.QueryConverter.VisitWhere(Expression sequence, LambdaExpression predicate) +145
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +3145
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1091
   System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector) +41
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +1292
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1091
   System.Data.Linq.SqlClient.QueryConverter.VisitDistinct(Expression sequence) +24
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +3687
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1091
   System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector) +41
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +1292
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1091
   System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(Expression sequence, LambdaExpression lambda, SqlNodeType aggType, Type returnType) +93
   System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc) +4511
   System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1091
   System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node) +79
   System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) +211
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +149
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +42
   System.Web.Query.Dynamic.DynamicQueryable.Count(IQueryable source) +209
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelectAutoSortAndPage(IQueryable source, DataSourceSelectArguments arguments) +153
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelectQuery(LinqDataSourceSelectEventArgs selectEventArgs, Object selectResult, Object table, Boolean storeOriginalValues) +745685
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +509
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +27
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.ListView.PerformSelect() +74
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.ListView.CreateChildControls() +74
   System.Web.UI.Control.EnsureChildControls() +146
   System.Web.UI.Control.PreRenderRecursiveInternal() +61
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

Open in new window

Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

The problem is that you are not "selecting" the item. You are just clicking on it. Add a button and in its "CommandName" put "Select"
Avatar of luscoma

ASKER

I just tried that, the problem is it postsback like you'd expect but doesn't show the selecteditemtemplate.  I feel like this is due to that exception (which shows up if I add lvCatagories.SelectedIndex = 0 in an ondatabound event).  This makes me believe its not picking up the selecteditemtemplate because of that exception
Avatar of luscoma

ASKER

Side note if you want to see the current state of it, its at http://www.motionmedical.com/physician/catalog.aspx (its not styled or anything like that just an accordion control with a listview in it)  It doesn't show that exception right now cause I took out the SelectedIndex = 0 part so it would load.
Here is what you have to do:
1. The select button must be like the following:

<asp:Button ID="btnSelect" runat="server" CommandName="Select" CommandArgument="" />
<?xml:namespace prefix = asp />2. Use the event SelectedIndexChanging of the ListView and implement the following inside it:

    protected void lvCategories_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
    {
        lvCategories.SelectedIndex = e.NewSelectedIndex;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Salim Fayad
Salim Fayad
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luscoma

ASKER

You are perhaps my hero....