Reputation: 2337
I have LinqDataSource that being attached to GridView In Asp.net web application.
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" OnInit="LinqDataSource1_Init"
EnableDelete="True"
EntityTypeName="Id" TableName="CompanysCourses"
OnDeleting="Course_Deleting" OnDeleted="Course_Deleted">
</asp:LinqDataSource>
Within OnInit event I have modified the select and where properties like that
this.LinqDataSource1.Where = "CompanyId == " + UserManager.CompanyId;
this.LinqDataSource1.Select= "Course";
I am selecting table named CompanysCourses and after that filter the result and select specific courses based on company's ID
And this is my grid view
<asp:GridView ID="grdVwCourses" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="LinqDataSource1"
OnSelectedIndexChanged="grdVwCourses_SelectedIndexChanged"
CssClass="table table-responsive"
PageSize="10" GridLines="Horizontal"
meta:resourcekey="GridView1Resource1">
<EmptyDataTemplate>
<asp:Label runat="server" ID="NotFoundLabel" Text="" meta:resourcekey="NotFoundLabelResource1"></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="#" meta:resourcekey="HeaderId">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" Visible="false"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource1" />
<asp:BoundField DataField="EnglishName" HeaderText="EnglishName" SortExpression="EnglishName" meta:resourcekey="BoundFieldResource2" />
<asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" SortExpression="CreatedDate" meta:resourcekey="BoundFieldResource3" />
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:HyperLink ID="userLink" runat="server" NavigateUrl='<%# GetCategoriesLink(Eval("Id")) %>' Text="Categories" meta:resourcekey="userLinkResource1" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="True" meta:resourcekey="CommandFieldResource1" />
</Columns>
</asp:GridView>
I have in the grid view enabled deleting but when click delete I got this exception
LinqDataSource 'LinqDataSource1' does not support the Select property when the Delete, Insert or Update operations are enabled.
I have override On_Deleting event like that to show confirmation dialog
protected void Course_Deleting(object sender, LinqDataSourceDeleteEventArgs e)
{
e.Cancel = true
this.panMessage.Visible = true;
int id = ((Course)e.OriginalObject).Id;
ViewState["DeleteObject"] = id;
}
I wonder what is the reason of this problem.
Edit The question referred to as duplicate to mine doesn't have the answer that I seek, also the details are different.
Upvotes: 0
Views: 703
Reputation: 4340
I came today to the same issue on some legacy project, as the error showing this widget doesn't support deleting when you use SELECT statement.
so the solution is to remove your select statement from this datasource, ie remove this line:
this.LinqDataSource1.Select= "Course";
then on the gridview change DataKeyNames to your table Id name and also change the navigation property to access the fields, for example Course.Id, Course.Name etc for each columns in the fields.
the last thing is to cast to the main table and use the any field to access the Id of that table child value.
int id = ((Course)e.OriginalObject).Id;
Upvotes: 0