Paging error with EntityDataSource - asp.net

I'm using an EntityDataSource and creating a projection using the CommandText property to query across several tables. I want to allow paging, but when I run the code I get an error that says
For the EntityDataSource, if the query specifies a projection and
paging is enabled, a sort expression must be defined. Either set the
OrderBy property or set AutoGenerateOrderByClause to true
Odd thing is, I HAVE set the AutoGenerateOrderByClause to true and the error persists. I'm not sure what to do at this point. Here's my example EntityDataSource code.
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>
Anyone know how to make paging work with this?

You have a projection. Either get rid of the SELECT or also add OrderBy="it.astID"

Just to clarify the accepted answer of AFD.
Change the EntityDataSource to the following:
<asp:EntityDataSource runat="server" ID="EntityDataSource"
ConnectionString="name=AssetRegistryEntities"
DefaultContainerName="AssetRegistryEntities"
OrderBy="it.astID"
CommandText="SELECT a.astName, a.astDescription, r.rolFK_adCN
FROM AssetRegistryEntities.Assets as a
JOIN AssetRegistryEntities.Roles as r ON r.rolFK_astID = a.astID
WHERE r.rolFK_adCN = 'dpellerin'
AND r.rolTypeCode = 'PRIANALYST'"
AutoGenerateOrderByClause="true">
</asp:EntityDataSource>

Related

SqlDataSource select statement syntax error

I'm working on an ASP page for a project, which should provide an interface for the user to maintain information on local little league baseball and softball. I keep getting an error:
Syntax error in FROM clause.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
when attempting to use a SqlDataSource with the following SelectCommand:
SELECT tblPlayers.PlayerCode, tblPlayers.First_Name,
tblPlayers.Last_Name, tblPlayers.Gender,
tblPlayers.Date_of_Birth, tblPlayers.League_age,
tblPlayers.InActiveStatus,
tblParentMaster.ParentFirst+' '+tblParentMaster.Parentlast AS Parent_1,
tblParentMaster_2.ParentFirst+' '+tblParentMaster_2.ParentLast AS Parent_2,
tblPlayers.Birth_Certificate_on_file
FROM tblPlayers
JOIN tblParentMaster
ON tblPlayers.HOHCode = tblParentMaster.ParentCode
JOIN tblParentMaster AS tblParentMaster_2
ON tblPlayers.Parent2Code = tblParentmaster_2.ParentCode
WHERE (tblPlayers.PlayerCode = #PlayerCode)
In this code, #PlayerCode is the SelectedValue attribute of a GridView control on the page. I've already declared the ControlParameter for the SqlDataSource with the above SelectCommand:
<SelectParameters>
<asp:ControlParameter ControlID="selectPlayerGridView" Name="PlayerCode"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
The SQL select statement is being used to populate data into a FormView. I'm not bothering to post the code for the FormView, sine it's a large chunk of code. I'm not brand new to SQL, but I don't have a lot of practical experience, either. Can someone please point out the syntax error? I appreciate any help, thanks.
AS you said in comment
If you are using .mdb file then it is an MS Access database then It should be connected using OleDb providers
and then your query should be like the following
SELECT tblPlayers.PlayerCode, tblPlayers.First_Name,
tblPlayers.Last_Name, tblPlayers.Gender,
tblPlayers.Date_of_Birth, tblPlayers.League_age,
tblPlayers.InActiveStatus,
tblParentMaster.ParentFirst & ' ' & tblParentMaster.Parentlast AS Parent_1,
tblParentMaster_2.ParentFirst & ' ' & tblParentMaster_2.ParentLast AS Parent_2,
tblPlayers.Birth_Certificate_on_file
FROM tblPlayers
JOIN tblParentMaster
ON tblPlayers.HOHCode = tblParentMaster.ParentCode
JOIN tblParentMaster AS tblParentMaster_2
ON tblPlayers.Parent2Code = tblParentmaster_2.ParentCode
WHERE (tblPlayers.PlayerCode = #PlayerCode)
Replace + with &

Selecting values between 2 dates

I am currently working on a website that offers tutoring services and I have been stuck on this issue quite a while..
I have 2 text boxes where the user chooses a start date and a finish date, when the user clicks view he would be suppose to see the results in a gridview. I am using FormParameters to insert the date into the query.
SelectCommand of the sqldatasource
SelectCommand="SELECT [Session].Session_num AS Num, [Session].Session_Time_Stamp AS Session_Date, Student.Student_First & ' ' & Student.Student_Last AS Student FROM (([Session] INNER JOIN Student ON [Session].Student_Num = Student.Student_Num) INNER JOIN Class ON [Session].Class_ID = Class.Class_ID) WHERE ((([Session].Session_Time_Stamp) Between #firstdate And #seconddate))">
Parameters of the SqlDataSource
<SelectParameters>
<asp:FormParameter Name="firstdate" Type="DateTime"/>
<asp:FormParameter Name="seconddate" Type="DateTime"/>
</SelectParameters>
This is executed when the user clicks the view button, it is where I set the values of the parameters and execute the sql select.
Dim fdate As DateTime = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")
Dim ldate As DateTime = Format(CDate(txtEndDate.Text), "MM/dd/yyyy")
gridTutor.SelectParameters("firstdate").DefaultValue = fdate
gridTutor.SelectParameters("seconddate").DefaultValue = ldate
gridTutor.Select(DataSourceSelectArguments.Empty)
gridTutorSessions.DataBind()
fdate and ldate are not empty, however after this is executed the query result is empty. Could it be that wrong methods to execute the select?
Edit: I realized the problem is probably with the query and DateTime format. When I transformed my textbox value to DateTime it put it like this #2/20/2014#. However, it doesn't return anything even if there are values between the two dates.
If anybody have an access query with DateTime I would like to see it. Thanks
I managed to fix it by formatting the date in my access database it's not the best solution but it is a fix for the situation
I believe you need to manually set fdate and ldate just before you do your 'selectparameters' (i.e. use "fdate = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")". According to the following, values asigned to Dim in the manner you have would not reflect the realtime values you want. See the following regarding VB: "You can assign a value to a variable when it is created. For a value type, you use an initializer to supply an expression to be assigned to the variable. The expression must evaluate to a constant that can be calculated at compile time.

asp.net dropdown null value stores 0 instead of null

I'm trying to add a title for dropdown menu.
The problem is that if the data is null, it automatically update as 0.
This doesn't happen when I create the data row.
The date type is int32.
The funny thing is that I can insert null but I can not update as null.
My solution was to add two listitems.
One is value="". This is for initial data. (create)
Another is value="0". This is for after update.
However this is not pretty solution.
How come integer stores 0 instead of null when I update the data?
It seems possible to store null when I create the row...
<asp:DropDownList ID="addl" runat="server" SelectedValue='<%# Bind("a") %>'>
<asp:ListItem Value="" Text="-Select-"></asp:ListItem>
<asp:ListItem Value="1">test1</asp:ListItem>
<asp:ListItem Value="2">test2</asp:ListItem>
<asp:ListItem Value="0" Text="-Select-"></asp:ListItem>
</asp:DropDownList>
what would be the best solution for dropdown menu title if the data type is integer?
If it's string, I can simply add value="" and works just time.
Integer stores 0 if it's null and I have to also create null in order to avoid error in edit mode.
Int32 is not a Nullable type and by default will equal 0 (even if not initialized). Try using Int32? which is Nullable, or use some other index to indicate the Create option.
I'm not quite sure what int you are trying to update, but if it's a variable that you have control over you could look at changing it from a standard int to a nullable int. This can be as simple as changing this:
int i;
to this:
int? i;

How to sort with a CASE statement in an EntityDataSource?

I'm using a CASE statement in my EntityDataSource to do custom sorting. Consider the following code:
<asp:EntityDataSource ID="myEntityDataSource" runat="server"
ConnectionString="name=MySQLEntities1"
DefaultContainerName="MySQLEntities1"
EnableFlattening="False"
EntitySetName="Persons"
EntityTypeFilter="Persons"
OrderBy="it.[Pack],
CASE it.[Type]
WHEN 'MAN' THEN 1
WHEN 'VROUW' THEN 2
WHEN 'KIND' THEN 3
END,
it.[BirthDate] ASC" />
In T-SQL this would be a perfecty normal way of sorting, but used in the EntityDataSource it throws the following exception:
The query syntax is not valid. Near identifier 'it', line 11, column
21.
How can I get this type of sorting to work in my EntityDataSource?
I was trying the very same thing today. I discovered on the Entity SQL Reference site that you apparently have to use CASE WHEN and cannot use CASE [value] WHEN. This approach, although not as it would be in T-SQL, did work for me. So your code should look like this:
<asp:EntityDataSource ID="myEntityDataSource" runat="server"
ConnectionString="name=MySQLEntities1"
DefaultContainerName="MySQLEntities1"
EnableFlattening="False"
EntitySetName="Persons"
EntityTypeFilter="Persons"
OrderBy="it.[Pack],
CASE
WHEN it.[Type] = 'MAN' THEN 1
WHEN it.[Type] = 'VROUW' THEN 2
WHEN it.[Type] = 'KIND' THEN 3 END, it.[BirthDate] ASC" />
MSDN Entity SQL Reference: 'CASE'

Bind check on column

I'm pretty new with VB & ASPX.NET.
But I'm having a problem with showing the right info in a gridview.
Basically I've got a maintenance tool for an application. And I want to make it multi language.
This is a check for the language:
'setting the column name where to get the text resource from
Dim comment As String = "comment"
If (licentie.getlanguage() = "NL") Then
comment = "comment_NL"
End If
'Part of the sql query
sqlDataSourceGridView.SelectCommand = "select inst.categorie,inst.term_id,inst.term_result,inst." + comment + ", ETC.....
So far this works. But in my template I've got the following code in the gridview:
<asp:Label ID="LabelType" runat="server" Text='<%# Bind("Comment") %>' />
So my question is, How do I set the column 'comment_NL' when the language is set 'NL' in the bind?
Just give a Column Alias for inst.comment and inst.comment_NL by using AS in your Select Query
select inst.categorie,inst.term_id,inst.term_result,inst." + comment + " AS Comment, ETC.....

Resources