View Single Post
  #30 (permalink)  
Old April 15th, 2010, 06:58 AM
martinspalding martinspalding is offline
Registered User
 
Join Date: Mar 2010
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error

The error I receive is the one I received originally: "Can't bind a datasource without a valid ItemId"?

What I've done is I've altered my ItemDataBound event to accomodate for each list items rating values...

Code:
protected void lvTrustAccounts_ItemDataBound(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)
                {
                    Object dataRecord = ((ListViewDataItem)e.Item).DataItem; // Get the dataitem and place it into dataRecord
                    Type anonType = dataRecord.GetType(); // get the anonymous type

                    // Get user id and rating values using reflection
                    Guid userid = (Guid)anonType.GetProperty("UserId").GetValue(dataRecord, null);
                    int rating1 = (int)anonType.GetProperty("Rating01").GetValue(dataRecord, null);
                    int rating2 = (int)anonType.GetProperty("Rating02").GetValue(dataRecord, null);
                    int rating3 = (int)anonType.GetProperty("Rating03").GetValue(dataRecord, null);
                    int rating4 = (int)anonType.GetProperty("Rating04").GetValue(dataRecord, null);
                    int rating5 = (int)anonType.GetProperty("Rating05").GetValue(dataRecord, null);

                    // Put rating values in array
                    int[] rateValues = new int[] 
                    { 
                       rating1, rating2, rating3, rating4, rating5 
                    };

                    myRating.ItemId = myTraders.Id;
                    myRating.DataSource = myTraders.Rating;
                }
            }
        }
    }
and instead of using the ObjectDataSource to populate my listview I am using the below LinqDataSource code...

Code:
<asp:LinqDataSource 
                                            ID="TrustsDataSource" 
                                            runat="server" 
                                            ContextTypeName="TradeSelectorDataContext"
                                            EnableDelete="True" 
                                            EnableInsert="True" 
                                            EnableUpdate="True"
                                            TableName="tblTraders" 
                                            OnSelecting="TrustsDataSource_Selecting"/>
With my LinqDataSources OnSelecting event code holding my query...

Code:
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
             //join tc in db.tblCustomers on cj.UserId equals tc.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;
    }
Any help appreciated.