DataBinding: 'DynamicClass1' does not contain a property with the name 'EmployeeID' - asp.net

i have used aggregate function in List Page of dynamic datawebsite. When it execute it gives me,
DataBinding: 'DynamicClass1' does not contain a property with the name 'EmployeeID'.
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource"
AllowPaging="True" AllowSorting="True" CssClass="gridview">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="EditHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>'
Text="Edit" /> <asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete"
CausesValidation="false" Text="Delete"
OnClientClick='return confirm("Are you sure you want to delete this item?");'
/> <asp:HyperLink ID="DetailsHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Details, GetDataItem()) %>'
Text="Details" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="footer"/>
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
<asp:LinqDataSource ID="GridDataSource" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="Employees"
GroupBy="EmployeeNo"
Select="new(Key,
Max(Version) As VersionNo)"
EnableDelete="true">
<WhereParameters>
<asp:DynamicControlParameter ControlID="FilterRepeater" />
</WhereParameters>
</asp:LinqDataSource>
I have not changed any of the default code except i have added ContextTypeName,TableName, GroupBy and select in Linq Data source...
Table "Employees" has "EmployeeID, EmployeeNo, EmployeeName, Department, Address, City, State, Country, Version" as columns..
Any idea how to solve this?
Thanks in advance!
Regards,
Bala

The problem is that you're grouping doesn't create the type of data that the grid is expecting to receive. GetDataItem() is expecting a particular type of data and that's not what you've set up in your LinqDataSource.
Your select needs to include all the items that GetDateItem() needs, right now it doesn't:
new(Key, Max(Version) As VersionNo) //EmployeeID needs to be included here

new(Key, Max(Version) As VersionNo) is fine.
To Get the Employee... use: Key.Employee
The Key is a dynamic class containing all the properties specified in the group by statement.
Anything else (ie EmployeeNo, EmployeeName, Department, Address, City, State, Country, Version) will need an aggregate statement (ie Sum, Max, Min).

Related

Update gridview: Could not find control 'gvname$chkBool' in ControlParameter 'parameter'.

I'm trying to get a checkbox in a gridview to be an update parameter in a gridview. The field I want updated, author_approved_updated, is a binary field and sometimes it's null so I had to create a field in the select query string to make sure it always returns a non-null value.
But every time I try the code I get the error message about how it can't find the control in the control parameter. I checked here and many people suggested putting $ between the gridview name and the parameter name. Sadly, that doesn't work either.
Could not find control 'gvSops$chkAAU' in ControlParameter 'author_approved_updated'.
<asp:GridView runat="server" ID="gvSops" DataSourceID="dsSops" AutoGenerateColumns="false" AllowSorting="true" AutoGenerateEditButton="true" DataKeyNames="sopID">
<Columns>
...
<asp:TemplateField HeaderText="Author Approved Updated" SortExpression="author_approved_updated">
<EditItemTemplate>
<asp:CheckBox runat="server" ID="chkAAU" Checked='<%# Bind("boolAAU") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAAU" Checked='<%# Bind("boolAAU") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
...
</gridview>
<asp:SqlDataSource ID="dsSops" runat="server" ConnectionString="<%$ ConnectionStrings:AMS %>"
SelectCommandType="Text"
SelectCommand="SELECT *, isnull(author_approved_updated, 0) as boolAAU FROM AMS_SOPs"
UpdateCommandType="Text"
updatecommand="Update AMS_SOPs SET
author_approved_updated = #author_approved_updated
WHERE (sopID = #sopID)"
>
<UpdateParameters>
<asp:ControlParameter ControlID="gvSops$chkAAU" PropertyName="Checked" Name="author_approved_updated" Type="Boolean" ConvertEmptyStringToNull="false" />
</UpdateParameters>
I'm not sure what else to do. Does the fact the variable in the bind function doesn't match up to the filed in the database query? Would this work better as a stored procedure instead of and update command? Does anyone know how to fix it?
Thanks in advance!

How to add a tooltip to Boundfields in a Detailsview, but only if color of column has changed

I have the following code ...
<asp:DetailsView ID="dvApprenticeship" runat="server" DataSourceID="dsApprenticeship" AutoGenerateRows="false" BackColor="#E0E8F0" GridLines="None" CellPadding="2"
DataKeyNames="ProgramID, ProgramName, OrganisationName, StudyYearID, Workgroup, Pathway, FinishDate" OnDataBound="Apprenticeship_DataBound">
<Fields>
<asp:BoundField DataField="ProgramName" HeaderText="Program:" />
<asp:BoundField DataField="StudyYearName" HeaderText="Study Year:" />
<asp:HyperLinkField DataTextField="OrganisationName" HeaderText="Apprenticeship: " NavigateUrl="Apprenticeships.aspx" />
<asp:BoundField DataField="Workgroup" HeaderText="Workgroup:" />
<asp:BoundField DataField="Pathway" HeaderText="Pathway:" />
<asp:TemplateField HeaderText="Nominal Completion: ">
<ItemTemplate>
<asp:Label ID="labEndDate" runat="server" Text='<%# Eval("FinishDate","{0:d/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterTemplate>
<asp:LinkButton ID="lbAddProgramUnits" runat="server" OnClick="AddProgramUnits_Click" ForeColor="White" Font-Bold="true"
OnClientClick="return confirm('Import the Program Units listed - this may overwrite unit dates. Are you sure?');">Import from Program</asp:LinkButton>
Help
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" BackColor="LightSlateGray" />
</asp:DetailsView>
I want to be able to show a tooltip whenever one of the above Boundfields has changed color.
In my C# codebehind, I have code that changes the color of these boundfields depending on certain conditions of the data. This is working fine.
But what I want is to be able to give the users a tooltip when ever they hover their mouse over these Boundfields and ONLY if that field is coloured differently, in my case
color.Yellow
.
And to answer my own question, with something else I found, which was overlooked: the convert BoundField to TemplateField option.
From this ...
<asp:BoundField HeaderText="Claim Type ID" ..etc../>
To This ...
<asp:TemplateField HeaderText="Claim Type ID">
<EditItemTemplate>
<asp:Label ID="lblClaimTypeID" runat="server" Text='<%# Eval("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="itClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="A numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is sweet, because in the Design mode of ASPX, you can select your DetailsView, select the Edit Fields option and select the fields that are BoundFields and convert them straight into TemplateFields. The beauty with that, is that it converts the BoundFields to neat Labels or TextBoxes allowing you to directly code in a ToolTip property! And no code behind! Microsoft got something right there for once.
If you are setting the color to yellow in the DetailsView DataBound event based on some criteria, you can set the tooltip in that same block:
DetailsViewRow.Cells[indexofyellowfield].ToolTip = "some help from code-behind";

ASP.NET Dynamic Data Sorting on IDForeign Key

In my application we are using ASP.NET Dynamic Data for data binding. Everything works fine.
If you see the code snipped below, it has Dynamic Field DepartmentID which is a foreign key to Department table and gets the department name from there (this is specified in DynamicData/FieldTemplates/IDForeignKey.ascx.cs).
Our requirement is to sort (order by) the records on the department name as they are visible on the grid view. The only option that I have is to sort on DepartmentID which does not serve our purpose.
How can we sort on data that is retrieved from ForeignKey?
List.aspx
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" OnPreRender="GridView1_PreRender"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="EditHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>'
Text="Edit" /> | <asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete"
CausesValidation="false" Text="Delete"
OnClientClick='return confirm("Are you sure you want to delete this item?");'
/>
</ItemTemplate>
</asp:TemplateField>
<asp:DynamicField DataField="DepartmentID" HeaderText="Department" />
<asp:DynamicField DataField="Description" HeaderText="Description"/>
</Columns>
<PagerStyle/>
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
</div>
<asp:EntityDataSource OnSelecting="GridDataSource_Selecting" ID="GridDataSource" runat="server" EnableDelete="true" OnDeleted="GridDataSource_Deleted" >
<WhereParameters>
<asp:DynamicControlParameter ControlID="DynamicFilter1" />
</WhereParameters>
</asp:EntityDataSource>
Unfortunately it is not entirely clear from your question how are describe your metadata,
but anyway you can try to use QueryExtender control and SearchExpression class. More detail information you can find by link QueryExtender and SearchExpression with examples.
The whole idea of this is use custom LINQ.

asp:EntityDataSource failing to update or delete gridview

I have a gridview of vendor bids that have foreign keys to a vendors table and a product table.
Here is my gridview and my datasource.
<table>
<asp:GridView ID="GridViewVendorBids" runat="server" AutoGenerateColumns="False"
DataSourceID="VendorBidsDS" DataKeyNames="autRecNum" ForeColor="#333333" CellPadding="4"
GridLines="None" AllowPaging="True" AllowSorting="True">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="dtmBidDate" HeaderText="Bid Date" SortExpression="dtmBidDate"
DataFormatString="{0:MM/dd/yyyy}" ApplyFormatInEditMode="true" />
<asp:TemplateField HeaderText="Vendor Name" SortExpression="it.tblVendors.strVendorName">
<ItemTemplate>
<asp:Label ID="lblVendorName" runat="server" Text='<%# Eval("tblVendors.strVendorName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlVendorList" runat="server" DataTextField="strVendorName"
DataSource='<%# getVendorList() %>' DataValueField="strVendorCode" AutoPostBack="false"
SelectedValue='<%# Bind("strVendorCode") %>' DropDownStyle="DropDownList" AutoCompleteMode="SuggestAppend"
CssClass="comboBoxInsideModalPopup" Width="250px" MaxLength="0" Style="display: inline;
top: 0px; left: 0px;" ItemInsertLocation="Append" RenderMode="Inline" Height="16px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Name" SortExpression="it.tblProducts.strProductName">
<ItemTemplate>
<asp:Label ID="lblProductName" runat="server" Text='<%# Eval("tblProducts.strProductName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProductList" runat="server" DataTextField="strProductName"
DataSource='<%# getProductList() %>' DataValueField="strProductCode" AutoPostBack="false"
SelectedValue='<%# Bind("strProductCode") %>' DropDownStyle="DropDownList" AutoCompleteMode="SuggestAppend"
CssClass="comboBoxInsideModalPopup" Width="300px" MaxLength="0" Style="display: inline;
top: 0px; left: 0px;" ItemInsertLocation="Append" RenderMode="Inline" Height="16px" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit" SortExpression="it.tblProducts.strUnitDesc">
<ItemTemplate>
<asp:Label ID="lblUnitDesc" runat="server" Text='<%# Eval("tblProducts.strUnitDesc") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sngBid" HeaderText="Bid" SortExpression="sngBid" ItemStyle-HorizontalAlign="Right"
DataFormatString="{0:0.00}" />
<asp:BoundField DataField="strBidType" HeaderText="Bid Type" SortExpression="strBidType"
ReadOnly="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</table>
<asp:EntityDataSource ID="VendorBidsDS" runat="server" ConnectionString="name=IMSEntities"
DefaultContainerName="IMSEntities" EntitySetName="tblVendorBid" Include="tblVendors,tblProducts"
EnableFlattening="False" OrderBy="it.dtmBidDate DESC" EnableDelete="true" EnableUpdate="true">
</asp:EntityDataSource>
When I press the update or delete key I get the following error:
No key property values were found during an update or delete operation. Check to ensure that key properties specified as binding expressions are available to the data source.
I found this post which is very similar to my problem but when I tried the answer I still get the same error.
Any help is appreciated. Thanks.
For the entity datasource you will need a primary key for the table.it allows you to change the records...After adding the primary key right click on entity datasource & select update database
Is "autRecNum" the only primary key of your VendorBidsDS Table?
You may check your Data Source Configuration whether primary key and foreign key are set properly. Also you may check if the Delete/Update Rule for FKey is set to Cascade if the other table is not updated.
And in " SelectedValue='<%# Bind("strProductCode") %>' ", "strProductCode" should be entity property instead of database column.
That's the few points I can remember from my previous project long long time ago.
Hope it can help you.
I got the "No key property values were found..." error because I was trying to save twice back to an EntityDataSource without a valid primary key. The StoreGeneratedPattern setting is correctly set to Identity and the first Insert ran correctly however the subsequent update fails - presumably because the primary key hasn't yet be set.
Basically I had manually inserted a row (using DbContext) and ran context.SaveChanges() but then my Telerik Grid translated the "Update" CommandName of the button to run another update command after the insert, causing this error. The error doesn't occur when updating existing rows, so it's very likely related to the primary key of a newly inserted row.
To solve the issue I simply set the CommandName of the button to "Cancel". My manual insert/update still runs, but then the Grid doesn't attempt another update operation and correctly closes the row I'm editing.

Link to records with a specific letter in a GridView

I have an ASP.NET GridView in a Web Form. This GridView using a SqlDataSource and utilizes paging and searching. For the sake of reference, here is the trimmed down version of it:
<asp:SqlDataSource ID="myDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM [Person] p, [Location] l WHERE p.[LocationID]=l.[ID] ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
/>
<asp:GridView ID="myGridView" runat="server" DataSourceID="myDataSource"
AllowPaging="true" AllowSorting="true" PageSize="25" AutoGenerateEditButton="true"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Person" SortExpression="Name">
<ItemTemplate>
<asp:Literal ID="nameLit" runat="server" Text='<%# Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Email" SortExpression="Email" HeaderText="Email Address" />
<asp:TemplateField HeaderText="Location" SortExpression="City">
<ItemTemplate>
<asp:Literal ID="cityLit" runat="server" Text='<%# Bind("City") %>' />
<asp:Literal ID="stateLit" runat="server" Text='<%# Bind("State") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The data source has a lot of records. At least 25,000. Because of this, I want to allow the user to filter by state. I want to provide a drop down list with a list of states. When a user changes the drop down list, the results in the grid view will be filtered by the selected state. However, I do not know how to do this with a SqlDataSource. Can someone please explain to me how to accomplish this?
Thank you!
First of all, let's add the DropDownList, and a SqlDataSource to populate it:
<asp:dropdownlist runat="server" id="StateDropDownList" DataSourceId="StateDataSource" DataTextField="State" DataValueField="State" />
<asp:sqldatasource runat="server" ConnectionString="<%$ ConnectionStrings:DB %>" SelectCommand="SELECT DISTINCT State FROM Location" />
Then we change your existing data source to use a State parameter and read the parameter's value from the DropDownList:
<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM
[Person] p INNER JOIN [Location] l ON p.[LocationID]=l.[ID]
WHERE (l.State = #State) ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
<SelectParameters>
<asp:ControlParameter ControlID="StateDropDownList" Name="State"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
/>
That should be all you need.
BTW, top tip: I changed your query slightly to use INNER JOIN syntax rather than doing the table join in a WHERE clause. There's some great info here on INNER JOIN.
Top tip #2: Paging in the GridView is a bit naive - every time you change pages, the GridView is reading all 25 000 records in your table, then throwing away the ones it doesn't need. There's a great article here (and plenty of questions on SO!) on doing custom paging with a Gridview.

Resources