This is a migrated thread and some comments may be shown as answers.

Linq, Databind error with Anonymous Types

5 Answers 168 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Simone
Top achievements
Rank 1
Simone asked on 05 May 2011, 10:16 PM
I get this error message:

 There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.

If I do:
Select sr => it works
Select sr, s.SessionNo => it does not work

If I use Anonymous types, it doesn't work at all

Link Query:
                           Dim query = From sr In db.SessionReps
                            From s In db.Sessions Where s.ID = sr.RepID And sr.RepID = repID
                            From g In db.Groups Where g.ID = s.GroupID
                            From p In db.Programs Where p.ID = g.ProgramID
                            Select New With {.ProgramName = p.Name, .GroupName = g.Name, sr.Active, sr.Attended, sr.DropOutDate, sr.ConfirmDate, sr.SessionID, s.Name, s.SessionNo, s.StartDate, s.EndDate}

                grdSessionReps.DataSource = query.ToList()  => datasource is correct.
                grdSessionReps.DataBind()    ***** ERROR here!!!


ASCX file:

        <Columns>
            <telerik:GridBoundColumn DataField="ProgramName" HeaderText="Program Name" UniqueName="ProgramName"
                ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="GroupName" HeaderText="Group Name" UniqueName="GroupName"
                ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridCheckBoxColumn DataField="Active" HeaderText="Active" ItemStyle-Width="50"
                DefaultInsertValue="true" UniqueName="Active" ReadOnly="True">
                <ItemStyle Width="50px"></ItemStyle>
            </telerik:GridCheckBoxColumn>
            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SessionNo" HeaderText="Session No" ReadOnly="True">
            </telerik:GridBoundColumn>
            <telerik:GridDateTimeColumn DataField="StartDate" HeaderText="Start Date" ItemStyle-Width="150"
                PickerType="DateTimePicker" UniqueName="StartDate" ReadOnly="True">
            </telerik:GridDateTimeColumn>
            <telerik:GridDateTimeColumn DataField="EndDate" HeaderText="End Date" ItemStyle-Width="150"
                PickerType="DateTimePicker" UniqueName="EndDate" ReadOnly="True">
            </telerik:GridDateTimeColumn>
            <telerik:GridCheckBoxColumn DataField="Attended" HeaderText="Attended" ItemStyle-Width="50"
                DefaultInsertValue="true" UniqueName="Active" ReadOnly="True">
                <ItemStyle Width="50px"></ItemStyle>
            </telerik:GridCheckBoxColumn>
            <telerik:GridDateTimeColumn DataField="DropOutDate" HeaderText="DropOut Date" ItemStyle-Width="150"
                PickerType="DateTimePicker" ReadOnly="True" UniqueName="DropOutDate">
            </telerik:GridDateTimeColumn>
            <telerik:GridDateTimeColumn DataField="ConfirmDate" HeaderText="Confirm Date" ItemStyle-Width="150"
                PickerType="DateTimePicker" ReadOnly="True" UniqueName="ConfirmDate">
            </telerik:GridDateTimeColumn>
        </Columns>

5 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 11 May 2011, 11:06 AM
Hi Simone,

I tried to reproduce the error you are getting, but was unsuccessful. Attaching the test page I used. Can you try to reproduce this issue in it?

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Shawn Clabough
Top achievements
Rank 1
answered on 11 Nov 2011, 02:27 AM
I am having the same problem as Simone using VB.  I've also attached screenshots showing that if I expand the .datasource property, it does have the datakeynames.

<telerik:RadGrid ID="rgCharges" runat="server" ShowFooter="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None">
    <ClientSettings>
        <Selecting AllowRowSelect="True" />
    </ClientSettings>
    <MasterTableView DataKeyNames="RegistrationChargePK">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="Amount" Aggregate="Sum" FilterControlAltText="Filter column column" HeaderText="Charges" UniqueName="Amount" DataFormatString="{0:c}">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="DateSubmitted" DataFormatString="{0:d}" DataType="System.DateTime" FilterControlAltText="Filter column1 column" UniqueName="DateSubmitted" HeaderText="Date">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ChargeType" FilterControlAltText="Filter column2 column" HeaderText="Charge Type" UniqueName="ChargeType">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Balance" FilterControlAltText="Filter column2 column" HeaderText="Balance" UniqueName="Balance">
            </telerik:GridBoundColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
    </MasterTableView>
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
    <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
    </HeaderContextMenu>
</telerik:RadGrid>


rgCharges.DataSource = From c In dc.RegistrationCharges Where c.School.SchoolPK = schoolID Order By c.DateSubmitted Select New With {.Amount = c.Amount, .DateSubmitted = c.DateSubmitted, .ChargeType = c.ChargeType, .Balance = c.Amount - c.Payments.Sum(Function(p) p.Amount)}
rgCharges.DataBind()
0
Veli
Telerik team
answered on 11 Nov 2011, 03:31 PM
Hi Shawn,

Are you sure you have properly selected all your data key fields in the LINQ query and they appear as properties of the anonymous type?

Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Shawn Clabough
Top achievements
Rank 1
answered on 11 Nov 2011, 06:00 PM
Veli,

Does the error1.jpg image not show you that the anonymous type has an Amount, DateSubmitted, ChargeType, and Balance property?

Also, if I bind the same results to a gridiview declared as below, I get Amount, DateSubmitted, ChargeType, and Balance headers for the columns with the expected data.

<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

Update:  I moved statement to the NeedDataSource method and it works.  Why would it work there, but not from another method?
0
Veli
Telerik team
answered on 14 Nov 2011, 02:42 PM
Shawn,

In your markup, I can see the following DataKeyNames declaration for the master table:

<MasterTableView DataKeyNames="RegistrationChargePK">

This requires that you have a field named RegistrationChargePK in your data objects. Simone in the first post shared the following error message:

There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.

In your first post on this thread, you confirm to be having the same problem:

I am having the same problem as Simone using VB.

This is what made me thing the RegistrationChargePK field not existing in your data source is the cause of the problem. Now it seems your scenario is different. Can you, please, clarify the exact issue and your particular scenario?

Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Simone
Top achievements
Rank 1
Answers by
Veli
Telerik team
Shawn Clabough
Top achievements
Rank 1
Share this question
or