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
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
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").Valu e _
}
e.Result = query
Get this error:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
here:
Select New With { _
Tania
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").Valu
}
e.Result = query
Get this error:
System.NullReferenceExcept
here:
Select New With { _
Tania
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thanks.
Getting same error:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Select New With {.Title = element.Attribute("title") .Value, _
T
Thanks.
Getting same error:
System.NullReferenceExcept
Select New With {.Title = element.Attribute("title")
T
Works fine for me => code as below
Can you show me your code
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
ASKER
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. LinqDataSo urceSelect EventArgs) Handles listsource.Selecting
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data /courseLis t.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
Not using Page_Load.
Its tied to linqdatasource /listview:
Protected Sub listsource_Selecting(ByVal
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data
Dim list = From element In xd.Descendants("product") _
Select New With {.Title = element.Attribute("title")
.Duration = element.Element("duration"
e.Result = list
End Sub
ASKER
Forgot to add:
LinqDataSource.Selecting event
LinqDataSource.Selecting event
so does that sorted you out ?
ASKER
no,
Protected Sub listsource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. LinqDataSo urceSelect EventArgs) Handles listsource.Selecting
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data /courseLis t.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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Protected Sub listsource_Selecting(ByVal
Dim xd As XDocument = XDocument.Load (Server.MapPath("/App_Data
Dim list = From element In xd.Descendants("product") _
Select New With {.Title = element.Attribute("title")
.Duration = element.Element("duration"
e.Result = list
End Sub
with the above get:
System.NullReferenceExcept
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="«" PreviousPageText="‹"
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="»" NextPageText="›"
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>
Also can you show me you xml (if different produced in this thread)
ASKER
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error:
Line 17: Dim xd As XDocument = XDocument.Load(Server.MapP ath("/App_ Data/cours eList.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\trai ning\test\ test2.aspx Line: 19
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.training_test_test2_as px._Lambda $__1(XElem ent element) in c:\websites\125344vg9\trai ning\test\ test2.aspx :19
System.Linq.<SelectIterato r>d__d`2.M oveNext() +185
System.Linq.Enumerable.Cou nt(IEnumer able`1 source) +202
lambda_method(ExecutionSco pe ) +48
System.Linq.EnumerableExec utor`1.Exe cute() +155
System.Linq.EnumerableExec utor`1.Exe cuteBoxed( ) +23
System.Linq.EnumerableQuer y`1.System .Linq.IQue ryProvider .Execute(E xpression expression) +75
System.Web.Query.Dynamic.D ynamicQuer yable.Coun t(IQueryab le source) +139
System.Web.UI.WebControls. DynamicQue ryableWrap per.Count( IQueryable source) +6
System.Web.UI.WebControls. LinqDataSo urceView.E xecuteSele ctAutoSort AndPage(IQ ueryable source, DataSourceSelectArguments arguments) +127
System.Web.UI.WebControls. LinqDataSo urceView.E xecuteSele ctQuery(Li nqDataSour ceSelectEv entArgs selectEventArgs, Object selectResult, Object table, Boolean storeOriginalValues) +308
System.Web.UI.WebControls. LinqDataSo urceView.E xecuteSele ct(DataSou rceSelectA rguments arguments) +482
System.Web.UI.DataSourceVi ew.Select( DataSource SelectArgu ments arguments, DataSourceViewSelectCallba ck callback) +17
System.Web.UI.WebControls. DataBoundC ontrol.Per formSelect () +149
System.Web.UI.WebControls. ListView.P erformSele ct() +56
System.Web.UI.WebControls. BaseDataBo undControl .DataBind( ) +70
System.Web.UI.WebControls. BaseDataBo undControl .EnsureDat aBound() +82
System.Web.UI.WebControls. ListView.C reateChild Controls() +53
System.Web.UI.Control.Ensu reChildCon trols() +87
System.Web.UI.Control.PreR enderRecur siveIntern al() +50
System.Web.UI.Control.PreR enderRecur siveIntern al() +170
System.Web.UI.Control.PreR enderRecur siveIntern al() +170
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +2041
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.NullReferenceExcept
Source Error:
Line 17: Dim xd As XDocument = XDocument.Load(Server.MapP
Line 18: Dim list = From element In xd.Descendants("product") _
Line 19: Select New With {.Title = element.Attribute("title")
Line 20: .Duration = element.Element("duration"
Line 21: e.Result = list
Source File: c:\websites\125344vg9\trai
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.training_test_test2_as
System.Linq.<SelectIterato
System.Linq.Enumerable.Cou
lambda_method(ExecutionSco
System.Linq.EnumerableExec
System.Linq.EnumerableExec
System.Linq.EnumerableQuer
System.Web.Query.Dynamic.D
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.DataSourceVi
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Ensu
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Page.Process
<%@ 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="«" PreviousPageText="‹"
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="»" NextPageText="›"
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>
ASKER
aaaahhhhhhhhhh - the agony of it!
it was the viewstate:
EnableViewState=true
All works just fine.
T
it was the viewstate:
EnableViewState=true
All works just fine.
T
cool.
Open in new window