SqlDataSource select statement syntax error - asp.net

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 &

Related

How to let a SqlDataSource use a list of numbers in SQL Server

Using asp.net I have a dropdown control on page that I want to use in my SqlDataSource without code behind.
My sql is
...WHERE ColumnID in (#Columns)
First values in dropdown are 1,3,4,5, so I want my SQL to become
..WHERE ColumnID in (1, 3, 4, 5)
Here is my control parameter
<SelectParameters>
<asp:ControlParameter ControlID="ddlColumns" Name="Columns" Type="String" PropertyName="SelectedValue" />
</SelectParameters>
I get an error :
Conversion failed when converting the nvarchar value '1,2,3,4,5' to data type int.
So it seems to be quoting
...WHERE ColumnID in ('1,3,4,5')
Is there anyway to tell it to not quote or to bypass (trick) it into working?

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.

Paging error with EntityDataSource

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>

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'

ASP.NET DataAdapter Query Not Valid While The Query Works

I started to use DataSet in my ASP.net web app like 6 months ago. It is a beautiful tool, allow me to rapidly develop MVC application without having to do all the dirty works in DB connection/queries.
But today I faced some weird problem. It started with this query:
select a.MR_PART_CODE as PART_CODE,
b.PART_DESC as PART_DESC,
b.PM_MAD_CAT_CODE as CATEGORY,
c.MPC_MIN_QTY as CAT_SS,
a.MR_MAX_LEAD_TIME as LEAD_TIME,
a.MR_MAD as MAD,
ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)) as CAL_SS,
greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) as SS,
d.SOH as SOH,
d.SOO as SOO,
(select sum(back_order) from STK_REQUEST where part_code=b.part_code) as BO,
(d.SOH+a.MR_SOO) as AVAIL,
((d.SOH + a.MR_SOO)-greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY)) as ROQ,
(d.SOH - greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) ) as VAR,
a.MR_REMARKS as REMARKS
from ROQ a, PART_MASTER b, MAD_PARTS_CATEGORY c, PART_STATS d
where a.MR_PART_CODE = b.PART_CODE
and d.PART_CODE = b.PART_CODE
and b.PM_MAD_CAT_CODE = c.MPC_CAT_CODE
and b.RETIRE_FLAG = 'N'
and a.mr_year = (select max(mr_year) from roq)
and a.mr_month = (select max(mr_month) from roq where mr_year= (select max(mr_year) from roq))
and a.mr_period = (select max(mr_period) from roq where mr_month=(select max(mr_month) from roq where mr_year= (select max(mr_year) from roq)) and mr_year= (select max(mr_year) from roq))
and greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) > d.SOH`
The query ran fine in Toad for Oracle, but apparently it fails when I tried to setup as a new query in DataAdapter object. It says something like "Error in list of function arguments: SELECT not recognized" to this line:
(select sum(back_order) from STK_REQUEST where part_code=b.part_code) as BO
What did I do wrong?
FYI, the database is Oracle.
It seems to be an exception from some ASP class trying to parse your SQL. There would be an ORA-xxxxx error number if the message came from Oracle.
You shouldn't put SQL like that in ASP. Create a view instead, perhaps it's ok then.

Resources