?
Solved

listview/linq - expand selection

Posted on 2009-04-29
14
Medium Priority
?
449 Views
Last Modified: 2013-11-11
Hi,
As a continuation of this thread:
http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_24362259.html#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

0
Comment
Question by:Tania_Farmer
  • 7
  • 7
14 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259020
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

0
 

Author Comment

by:Tania_Farmer
ID: 24259176
Hi,
Based on this:

http://www.experts-exchange.com/Programming/Languages/.NET/LINQ/Q_24127127.html#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

0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 2000 total points
ID: 24259188
Ok Tania he is VB version
 

Dim list = From element In xd.Descendants("product") _
                Select New With {.Title = element.Attribute("title").Value, _
                      .Duration = element.Element("duration").Value}

Open in new window

0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:Tania_Farmer
ID: 24259276
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
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259324
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

0
 

Author Comment

by:Tania_Farmer
ID: 24259360
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
0
 

Author Comment

by:Tania_Farmer
ID: 24259365
Forgot to add:

LinqDataSource.Selecting event
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259485
so does that sorted you out ?
0
 

Author Comment

by:Tania_Farmer
ID: 24259546
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.


0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259593
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

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259649
Also can you show me you xml (if different produced in this thread)
0
 

Author Comment

by:Tania_Farmer
ID: 24259752
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

0
 

Author Comment

by:Tania_Farmer
ID: 24259871
aaaahhhhhhhhhh - the agony of it!

it was the viewstate:

EnableViewState=true

All works just fine.

T
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24259880
cool.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question