Link to home
Start Free TrialLog in
Avatar of aspnet-scotland
aspnet-scotlandFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How do I use the linqdatasource?

Hi,

I have written the attached pages to populate my asp.net listview control from an objectdatasource. To allow for easier CRUD commands and to make my application more efficient I want to replace this objectdatasource with a linqdatasource but when I attempt this I receive a binding error which stops my ItemDataBound event firing? I'm basically replacing my objectdatasource with the below linqdatasource...

<asp:LinqDataSource
    ID="TrustsDataSource"
    runat="server"
    ContextTypeName="TradeSelectorDataContext"
    EnableDelete="True"
    EnableInsert="True"
    EnableUpdate="True"
    TableName="tblTraders"
    OnSelecting="TrustsDataSource_Selecting"/>

Where my OnSelecting event holds the below query...

protected void TrustsDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        TradeSelectorDataContext db = new TradeSelectorDataContext();

        var query =
           (
             from tt in db.tblTraders
             join ttcl in db.tblCityLookups on tt.city_id equals ttcl.city_id
             join lctt in db.tblCountryLookups on tt.country_id equals lctt.country_id
             join tcl in db.tblCountyLookups on tt.county_id equals tcl.county_id
             join twal in db.tblTraderWorkingAreaLookups on tt.trader_working_area_id equals twal.trader_working_area_id
             join ttrade in db.tblTraderTrades on tt.UserId equals ttrade.UserId
             join trade in db.tblTrades on ttrade.trade_id equals trade.trade_id
             join ttt in db.tblTradeTradeTypes on trade.trade_id equals ttt.trade_id
             join tradet in db.tblTradeTypes on ttrade.trade_type_id equals tradet.trade_type_id
             join cj in db.tblCustomerJobs on trade.trade_id equals cj.trade_id
             join tr in db.tblTraderRatings on tt.UserId equals tr.UserId
             orderby tt.trader_firstname ascending
             select new
             {
                 tt.UserId
               ,
                 tt.trader_title
               ,
                 tt.trader_firstname
               ,
                 tt.trader_surname
               ,
                 tt.trader_phone1
               ,
                 tt.trader_phone2
               ,
                 tt.trader_working_area_id
               ,
                 tt.trader_overall_rating
               ,
                 trader_working_area_name = twal.trader_working_area_name.Substring(0, twal.trader_working_area_name.IndexOf('(')).Trim()
               ,
                 trader_working_area_postcodes = twal.trader_working_area_name.Substring(twal.trader_working_area_name.IndexOf('(') + 1, twal.trader_working_area_name.LastIndexOf(')') - twal.trader_working_area_name.IndexOf('(') - 1).Trim()
               ,
                 trade_type_name = tradet.trade_type_name == null ? "" : tradet.trade_type_name
               ,
                 trade.trade_name
               ,
                 ttrade.trade_type_id
               ,
                 ttrade.trade_id
               ,
                 tr.Rating01
               ,
                 tr.Rating02
               ,
                 tr.Rating03
               ,
                 tr.Rating04
               ,
                 tr.Rating05
             }
           ).Distinct();

        e.Result = query;
    }

Am I placing my query within the wrong event? Why does my ItemDataBound event not fire?
***************DEFAULT.ASPX*****************

<%@ Page Title="" Language="C#" MasterPageFile="~/SingleColumn.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="Spaanjaars.Toolkit" Namespace="Spaanjaars.Toolkit" TagPrefix="isp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <div>
            <asp:Updatepanel ID="TrustAccountsUpdatePanel" runat="server" UpdateMode="Conditional">
                <contentTemplate>  
                    <div id="dlg" class="panelTrustAccounts" style="width:915px">
                        <div class="bodyTrustAccounts">
                            <div class="outerTrustAccounts">
                                <div class="innerTrustAccounts">
                                    <div class="contentTrustAccounts">
                                        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
                                        <asp:ObjectDataSource 
			                                ID="ObjectDataSource1" 
			                                runat="server" 
			                                SelectMethod="GetList" 
			                                TypeName="Traders">
			                            </asp:ObjectDataSource>
                                        <asp:ListView 
                                            ID="lvTrustAccounts" 
                                            runat="server"
                                            DataSourceID="ObjectDataSource1"
                                            DataKeyNames="Id"
                                            DataMember="DefaultView" 
                                            OnItemCreated="lvTrustAccounts_ItemCreated"
                                            OnItemDataBound="lvTrustAccounts_ItemDataBound">
                                            <LayoutTemplate>
                                                <table id="TrustAccounts" runat="server" class="gridTrustAccounts" cellspacing="0" border="0">
                                                    <tr>
                                                        <th><asp:LinkButton ID="btnSortTraderRating" runat="server" Text="Overall Rating" CommandName="Sort" CommandArgument="trader_overall_rating" Width="150px" /></th>
                                                    </tr>
                                                    <tr id="itemPlaceholder" runat="server" />
                                                </table>
                                            </LayoutTemplate>
                                            <EmptyDataTemplate>
                                                <table id="Table1" runat="server" style="">
                                                    <tr>
                                                        <td>No data was returned.</td>
                                                    </tr>
                                                </table>
                                            </EmptyDataTemplate>
                                            <ItemTemplate>
                                                <tr class='<%# (Container.DataItemIndex % 2 == 0)?"row":"altrow" %>' id="row" runat="server" style="height:108px;"> 
                                                    <td>
                                                        Book:
					                                    <asp:Label 
					                                        ID="Label1" 
					                                        runat="server" 
					                                        Text='<%# Eval("Name") %>'>
					                                    </asp:Label>
					                                    <isp:ContentRating 
					                                        ID="Rating1" 
					                                        runat="server" 
					                                        OnRated="ContentRating1_Rating" 
					                                        OnRating="ContentRating1_Rating" />
					                                    <br />
                                                    </td>
                                                 </tr>
                                            </ItemTemplate>
                                        </asp:ListView>
                                        <%--*****************************************************************************--%>
                                        <%--*****************************End List View***********************************--%>
                                        <%--*****************************************************************************--%>
                                    </div>
                                </div>
                            </div>
                        </div>                
                    </div>
                </contentTemplate>
            </asp:Updatepanel> 
        </div>
</asp:Content>

***********DEFAULT.CS******************

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using AjaxControlToolkit;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq.Expressions;
using System.Reflection;
using System.Data.SqlClient;
using System.Globalization;
using Spaanjaars.Toolkit;

public partial class _Default : System.Web.UI.Page
{
    public string thisConnectionString = ConfigurationManager.ConnectionStrings["TradeSelectorConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            FormsAuthentication.SetAuthCookie(HttpContext.Current.User.Identity.Name, false);
            Response.Redirect("Customers.aspx");
        }
    }

    protected void lvTrustAccounts_ItemDataBound(object sender, ListViewItemEventArgs e)   
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        
        //if (e.Item.ItemType == ListViewItemType.DataItem)
        if (e.Item.ItemType == ListViewItemType.DataItem)   
        {   
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;   
            if (myRating != null)   
            {
               Traders myTraders = dataItem.DataItem as Traders;
               if (myTraders != null)
               {
                   myRating.ItemId = myTraders.Id;
                   myRating.DataSource = myTraders.Rating;
               }
            }   
        }   
    }

    protected void lvTrustAccounts_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ContentRating myRating = e.Item.FindControl("Rating1") as ContentRating;
            if (myRating != null)
            {
                Traders myTraders = dataItem.DataItem as Traders;
                if (myTraders != null)
                {
                    myRating.ItemId = myTraders.Id;
                    myRating.DataSource = myTraders.Rating;
                }
            }
        }
    }

    protected void ContentRating1_Rating(object sender, RateEventArgs e)
    {
        ContentRating myRating = sender as ContentRating;
        Label2.Text = string.Format("ItemId: {0} Value {1}", myRating.ItemId, e.RateValue);
    }
}

******************TRADERS.CS********************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Traders
{
    public string Name { get; set; }
    public int[] Rating { get; set; }
    public int Id { get; set; }

    public List<Traders> GetList()
    {
        List<Traders> tempList = new List<Traders>();
        for (int i = 0; i < 5; i++)
        {
            Traders myTraders = new Traders();
            myTraders.Id = i;
            myTraders.Name = "Traders " + i.ToString();
            myTraders.Rating = new int[] { 0, 0, 0, 0, 0 };
            myTraders.Rating[i] = i + 1;
            tempList.Add(myTraders);
        }
        return tempList;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of XGIS
XGIS
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
Avatar of aspnet-scotland

ASKER

Full solution not provided