Link to home
Start Free TrialLog in
Avatar of Member_2_4426104
Member_2_4426104

asked on

listview/linq - expand selection

Hi,
As a continuation of this thread:
https://www.experts-exchange.com/questions/24362259/listview-xml-linqdatasource-need-help-data-not-showing.html?anchorAnswerId=24258710#a24258710

The code works but how do I expand it to include other items.

so would like to include duration and prices.
<courseList filedUnder="Courses">
  <category code="4wd" title="4wd Operation" >
    <product code="lr" title="4wd Bush Driving">
      <duration>1 Day</duration>
    </product>
    <product code="hr" title="4wd Defensive Driving">
      <duration>1 Day</duration>
    </product>
    <product code="sr" title="Self-Recovery">
      <duration>1 Day</duration>
    </product>
  </category>
</courseList>

Thanks
Tania
Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listsource.Selecting
		 
    Dim xd As XDocument = XDocument.Load(Server.MapPath("/App_Data/courseList.xml"))
    Dim list = (From x In xd.Descendants("product").Attributes("title") _
			Select New With {.Title = x.Value}).ToList() 
			 e.Result = list
 
End Sub
	 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
End Sub

Open in new window

Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia image

In C# its:
 

XDocument xd = XDocument.Load(@"c:\courseList.xml");
             var list = xd.Descendants("product").
                 Select(element => new
                                       {
                                           Title = element.Attribute("title").Value, 
                                           Duration = element.Element("duration").Value
                                       });
             e.Result = list;

Open in new window

Avatar of Member_2_4426104
Member_2_4426104

ASKER

Hi,
Based on this:

https://www.experts-exchange.com/questions/24127127/Linq-C-to-VB-conversion-select-new.html?anchorAnswerId=23595294#a23595294


Have converted it as follows:
      Dim query = From q In xd...<product> _
            Select New With { _
                              .title = q.Attribute("title").Value, _
                              .duration = q.Element("duration").Value _
                            }
                              
             e.Result = query

Get this error:
System.NullReferenceException: Object reference not set to an instance of an object.

here:
Select New With { _

Tania

ASKER CERTIFIED SOLUTION
Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia 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
Hi,
Thanks.

Getting same error:

 System.NullReferenceException: Object reference not set to an instance of an object.

Select New With {.Title = element.Attribute("title").Value, _


T
Works fine for me => code as below
Can you show me your code

Protected Sub Page_Load (ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim xd As XDocument
        xd = XDocument.Load ("c:\courseList.xml")
        Dim list = From element In xd.Descendants("product") _
                Select New With {.Title = element.Attribute("title").Value, _
                .Duration = element.Element("duration").Value}
    End Sub

Open in new window

Hi
Not using Page_Load.

Its tied to linqdatasource /listview:

Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listsource.Selecting
             
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data/courseList.xml"))
        Dim list = From element In xd.Descendants("product") _
                Select New With {.Title = element.Attribute("title").Value, _
                .Duration = element.Element("duration").Value}
e.Result = list

End Sub
Forgot to add:

LinqDataSource.Selecting event
so does that sorted you out ?
no,

Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listsource.Selecting
             
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data/courseList.xml"))
        Dim list = From element In xd.Descendants("product") _
                Select New With {.Title = element.Attribute("title").Value, _
                .Duration = element.Element("duration").Value}
e.Result = list

End Sub

with the above get:

System.NullReferenceException: Object reference not set to an instance of an object.


Works for me: <Code  Produced below>

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication2._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
 
    <form id="form1" runat="server">
    <div>
    
    </div>
    <script runat = server>
        Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listSource.Selecting
            Dim xd As XDocument = XDocument.Load("c:\courseList.xml")
            Dim list = From element In xd.Descendants("product") _
                    Select New With {.Title = element.Attribute("title").Value, _
                    .Duration = element.Element("duration").Value}
            e.Result = list
        End Sub
    </script>
    <asp:LinqDataSource 
      		ID="listSource" 
      		runat="server" 
      		OnSelecting="listsource_Selecting" 
      		 />
 
    <asp:ListView ID="ListView1" runat="server" DataSourceID = "listsource">
      <LayoutTemplate>
            <div id="datatable">
              <table id="Table1" border="1" class="prodtbl" runat="server">
                <tr class="mainheader">
                  <th > <asp:LinkButton ID="btnSortName" 
                       runat="server" CommandName="Sort" 
                       CommandArgument="title" Text="Course" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDuration" 
                     runat="server" CommandName="Sort" 
                     CommandArgument="title" Text="Duration" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDate" 
                      runat="server" CommandName="Sort" 
                      CommandArgument="title" Text="Date" />
                  </th>
                </tr>
                <tr id="itemPlaceholder" runat="server" /></tr>
              </table>
              <div class="pager">
                <asp:DataPager ID="pagerTop" runat="server" PageSize="20">
                  <Fields>
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         FirstPageText="&laquo;" PreviousPageText="&lsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="true" ShowPreviousPageButton="true" 
                         ShowLastPageButton="false" ShowNextPageButton="false"
                                            />
                    <asp:NumericPagerField
                         ButtonCount="5" NumericButtonCssClass="command" 
                         CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
                                            />
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         LastPageText="&raquo;" NextPageText="&rsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="false" ShowPreviousPageButton="false" 
                         ShowLastPageButton="true" ShowNextPageButton="true"
                                            />
                  </Fields>
                </asp:DataPager>
              </div>
            </div>
          </LayoutTemplate>
          <ItemTemplate>
            <tr id="row" >
               <td><%# Eval("title") %></td>
               <td><%# Eval("duration") %></td> 
              <td><%# Eval("title") %></td>
            </tr>
          </ItemTemplate>
 
    </asp:ListView>
    </form>
</body>
</html>

Open in new window

Also can you show me you xml (if different produced in this thread)
Hi,
something else must be going on - i get the same error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 17:             Dim xd As XDocument = XDocument.Load(Server.MapPath("/App_Data/courseList.xml"))
Line 18:             Dim list = From element In xd.Descendants("product") _
Line 19:                     Select New With {.Title = element.Attribute("title").Value, _
Line 20:                     .Duration = element.Element("duration").Value}
Line 21:             e.Result = list


Source File: c:\websites\125344vg9\training\test\test2.aspx    Line: 19

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.training_test_test2_aspx._Lambda$__1(XElement element) in c:\websites\125344vg9\training\test\test2.aspx:19
   System.Linq.<SelectIterator>d__d`2.MoveNext() +185
   System.Linq.Enumerable.Count(IEnumerable`1 source) +202
   lambda_method(ExecutionScope ) +48
   System.Linq.EnumerableExecutor`1.Execute() +155
   System.Linq.EnumerableExecutor`1.ExecuteBoxed() +23
   System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +75
   System.Web.Query.Dynamic.DynamicQueryable.Count(IQueryable source) +139
   System.Web.UI.WebControls.DynamicQueryableWrapper.Count(IQueryable source) +6
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelectAutoSortAndPage(IQueryable source, DataSourceSelectArguments arguments) +127
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelectQuery(LinqDataSourceSelectEventArgs selectEventArgs, Object selectResult, Object table, Boolean storeOriginalValues) +308
   System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +482
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +17
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +149
   System.Web.UI.WebControls.ListView.PerformSelect() +56
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +70
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.ListView.CreateChildControls() +53
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +50
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Control.PreRenderRecursiveInternal() +170
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041

<%@ Page Language="vb" AutoEventWireup="false"    %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
 
    <form id="form1" runat="server">
    <div>
    
    </div>
    <script runat = server>
        Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles listSource.Selecting
            Dim xd As XDocument = XDocument.Parse("<courseList filedUnder='Courses'>" & _
          "<category code='4wd' title='4wd Operation' >" & _
            "<product code='lr' title='4wd Bush Driving'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
            "<product code='hr' title='4wd Defensive Driving'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
            "<product code='sr' title='Self-Recovery'>" & _
              "<duration>1 Day</duration>" & _
            "</product>" & _
          "</category>" & _
        "</courseList>")
 
 
            Dim list = From element In xd.Descendants("product") _
                    Select New With {.Title = element.Attribute("title").Value, _
                    .Duration = element.Element("duration").Value}
            e.Result = list
        End Sub
    </script>
    <asp:LinqDataSource 
                ID="listSource" 
                runat="server" 
                OnSelecting="listsource_Selecting" 
                 />
 
    <asp:ListView ID="ListView1" runat="server" DataSourceID = "listsource">
      <LayoutTemplate>
            <div id="datatable">
              <table id="Table1" border="1" class="prodtbl" runat="server">
                <tr class="mainheader">
                  <th > <asp:LinkButton ID="btnSortName" 
                       runat="server" CommandName="Sort" 
                       CommandArgument="title" Text="Course" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDuration" 
                     runat="server" CommandName="Sort" 
                     CommandArgument="title" Text="Duration" />
                  </th>
                  <th> <asp:LinkButton ID="btnSortDate" 
                      runat="server" CommandName="Sort" 
                      CommandArgument="title" Text="Date" />
                  </th>
                </tr>
                <tr id="itemPlaceholder" runat="server" /></tr>
              </table>
              <div class="pager">
                <asp:DataPager ID="pagerTop" runat="server" PageSize="20">
                  <Fields>
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         FirstPageText="&laquo;" PreviousPageText="&lsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="true" ShowPreviousPageButton="true" 
                         ShowLastPageButton="false" ShowNextPageButton="false"
                                            />
                    <asp:NumericPagerField
                         ButtonCount="5" NumericButtonCssClass="command" 
                         CurrentPageLabelCssClass="current" NextPreviousButtonCssClass="command"
                                            />
                    <asp:NextPreviousPagerField 
                         ButtonCssClass="command" 
                         LastPageText="&raquo;" NextPageText="&rsaquo;" 
                         RenderDisabledButtonsAsLabels="true" 
                         ShowFirstPageButton="false" ShowPreviousPageButton="false" 
                         ShowLastPageButton="true" ShowNextPageButton="true"
                                            />
                  </Fields>
                </asp:DataPager>
              </div>
            </div>
          </LayoutTemplate>
          <ItemTemplate>
            <tr id="row" >
               <td><%# Eval("title") %></td>
               <td><%# Eval("duration") %></td> 
              <td><%# Eval("title") %></td>
            </tr>
          </ItemTemplate>
 
    </asp:ListView>
    </form>
</body>
</html>

Open in new window

aaaahhhhhhhhhh - the agony of it!

it was the viewstate:

EnableViewState=true

All works just fine.

T