ASP.NET DataAdapter Query Not Valid While The Query Works - asp.net

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.

Related

Mulesoft- Sql select connector query issue

I see the below error while connect and pull the records from the database through the Mule Database connector with an Oracle database. Can someone look at it and let me know what's wrong with this query.
Error:
"java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended\n"
type: DB:BAD_SQL_SYNTAX
Query:
SELECT A.sample_central_id,A.req_ndc_prod_id,B.lot_num,A.req_product_desc,A.req_hcp_fst_name,A.req_hcp_last_name,A.req_prof_desig,A.az_hcp_id,A.req_addr_line_1,A.req_addr_line_2,A.req_city,A.req_state_cd,A.req_zipcode,A.ffevnt_shipped_qty,A.ffevnt_ship_dt FROM scrf.ship_prod_hist_vw A,scrfval.ship_dtl B WHERE Trunc(A.req_sample_requested_dt)>= Trunc(SYSDATE - 30)AND Trunc(A.req_sample_requested_dt)<= Trunc(SYSDATE + 10)AND Trunc(A.ffevnt_ship_dt)>= Trunc(SYSDATE - 8)AND Trunc(A.ffevnt_ship_dt)<Trunc(SYSDATE - 1)AND Upper(A.lkp_brand_desc) LIKE '%LYNPARZA%' AND A.sample_central_id = B.sc_req_id AND A.ffevnt_ff_vendor_id = B.shipment_id AND A.req_ndc_prod_id = B.ndc_prod_id AND A.ffevnt_shipped_qty<>0 ORDER BY 1;
Try removing the semicolon (';') at the end.

Efficient joining the most recent record from another table in Entity Framework Core

I am comming to ASP .NET Core from PHP w/ MySQL.
The problem:
For the illustration, suppose the following two tables:
T: {ID, Description, FK} and States: {ID, ID_T, Time, State}. There is 1:n relationship between them (ID_T references T.ID).
I need all the records from T with some specific value of FK (lets say 1) along with the related newest record in States (if any).
In terms of SQL it can be written as:
SELECT T.ID, T.Description, COALESCE(s.State, 0) AS 'State' FROM T
LEFT JOIN (
SELECT ID_T, MAX(Time) AS 'Time'
FROM States
GROUP BY ID_T
) AS sub ON T.ID = sub.ID_T
LEFT JOIN States AS s ON T.ID = s.ID_T AND sub.Time = s.Time
WHERE FK = 1
I am struggling to write an efficient equivalent query in LINQ (or the fluent API). The best working solution I've got so far is:
from t in _context.T
where t.FK == 1
join s in _context.States on t.ID equals o.ID_T into _s
from s in _s.DefaultIfEmpty()
let x = new
{
id = t.ID,
time = s == null ? null : (DateTime?)s.Time,
state = s == null ? false : s.State
}
group x by x.id into x
select x.OrderByDescending(g => g.time).First();
When I check the resulting SQL query in the output window when executed it is just like:
SELECT [t].[ID], [t].[Description], [t].[FK], [s].[ID], [s].[ID_T], [s].[Time], [s].[State]
FROM [T] AS [t]
LEFT JOIN [States] AS [s] ON [T].[ID] = [s].[ID_T]
WHERE [t].[FK] = 1
ORDER BY [t].[ID]
Not only it selects more columns than I need (in the real scheme there are more of them). There is no grouping in the query so I suppose it selects everything from the DB (and States is going to be huge) and the grouping/filtering is happening outside the DB.
The questions:
What would you do?
Is there an efficient query in LINQ / Fluent API?
If not, what workarounds can be used?
Raw SQL ruins the concept of abstracting from a specific DB technology and its use is very clunky in current Entity Framework Core (but maybe its the best solution).
To me, this looks like a good example for using a database view - again, not really supported by Entity Framework Core (but maybe its the best solution).
What happens if you try to do a more straight forward translation to LINQ?
var latestState = from s in _context.States
group s by s.ID_T into sg
select new { ID_T = sg.Key, Time = sg.Time.Max() };
var ans = from t in _context.T
where t.FK == 1
join sub in latestState on t.ID equals sub.ID_T into subj
from sub in subj.DefaultIfEmpty()
join s in _context.States on new { t.ID, sub.Time } equals new { s.ID, s.Time } into sj
from s in sj.DefaultIfEmpty()
select new { t.ID, t.Description, State = (s == null ? 0 : s.State) };
Apparently the ?? operator will translate to COALESCE and may handle an empty table properly, so you could replace the select with:
select new { t.ID, t.Description, State = s.State ?? 0 };
OK. Reading this article (almost a year old now), Smit's comment to the original question and other sources, it seems that EF Core is not really production ready yet. It is not able to translate grouping to SQL and therefore it is performed on the client side, which may be (and in my case would be) a serious problem. It corresponds to the observed behavior (the generated SQL query does no grouping and selects everything in all groups). Trying the LINQ queries out in Linqpad it always translates to a single SQL query.
I have downgraded to EF6 following this article. It required some changes in my model's code and some queries. After changing .First() to .FirstOrDefault() in my original LINQ query it works fine and translates to a single SQL query selecting only the needed columns. The generated query is much more complex than it is needed, though.
Using a query from NetMage's answer (after small fixes), it results in a SQL query almost identical to my own original SQL query (there's only a more complex construct than COALESCE).
var latestState = from s in _context.States
group s by s.ID_T into sg
select new { ID = sg.Key, Time = sg.Time.Max() };
var ans = from t in _context.T
where t.FK == 1
join sub in latestState on t.ID equals sub.ID into subj
from sub in subj.DefaultIfEmpty()
join s in _context.States
on new { ID_T = t.ID, sub.Time } equals new { s.ID_T, s.Time }
into sj
from s in sj.DefaultIfEmpty()
select new { t.ID, t.Description, State = (s == null ? false : s.State) };
In LINQ it's not as elegant as my original SQL query but semantically it's the same and it does more or less the same thing on the DB side.
In EF6 it is also much more convenient to use arbitrary raw SQL queries and AFAIK also the database views.
The biggest downside of this approach is that full .NET framework has to be targeted, EF6 is not compatible with .NET Core.

SQL Query fails in Visual Studio

I need to display data on a chart from a database. As far as I can tell, the chart has to be bound to a DataSource which in my case is a SqlDataSource. My query to get this data is:
SELECT A.a, A.b, B.b, C.b
FROM (SELECT * FROM T WHERE T.c = 'str1') A,
(SELECT * FROM T WHERE T.c = 'str2') B,
(SELECT * FROM T WHERE T.c = 'str3') C
WHERE A.a = B.a AND A.a = C.a AND A.d = B.d AND A.d = C.d
This works fine in Oracle SQL Developer, but when configuring the SqlDataSource in Visual Studio, I can execute the query in the Query Builder, but it displays an error on the next page--the Test Query page. The error I get is:
There was an error executing the query. Please check the syntax of the command and if present, the types and values of the parameters and ensure they are correct.
Syntax Error: Expecting identifier or quoted identifier.
What's causing this error and how can I fix it?

Query is successfully executed in SQL Server but not in .NET

I wrote a query which is working fine in both .NET app and SQL Server.
But, when I was testing with wide parameters, I found that for that particular, it is not showing anything in .NET app but showing result in SQL Server.
I tried to google, no results with little strange. So, I am asking here.
This is my query:
SELECT DISTINCT
tblCustomers.customerID AS Customer#,
tblCustomers.firstName + ' ' + tblCustomers.surname AS Name,
tblCustomers.street AS Street,
tblCustomers.suburb AS Suburb,
tblCustomers.postCode AS Postcode,
tblCustomers.state AS State,
tblCustomers.country AS Country,
tblCustomers.phone AS [Phone No.],
tblCustomers.fax AS Fax,
tblCustomers.mobilePhone AS [Mobile Phone],
tblCustomers.email AS [E-mail]
FROM
tblCustomers
INNER JOIN
tblProduct_Backorder ON tblCustomers.customerID = tblProduct_Backorder.customerId
WHERE
(tblCustomers.customerID IN
(SELECT
customerId
FROM
tblProduct_Backorder AS tblProduct_Backorder_1
WHERE
(productId IN
(SELECT
productID
FROM
tblProducts
WHERE
(skuCode = 76761)
)
)
)
)
This query is not working for skuCode = 76761, but this one working fine in SQL Server.
Thanks.
You have really not enough information in you question for us to even start guessing what caused the problem. In the mean-time try this instead of your query:
SELECT c.customerID AS [Customer#],
c.firstName + ' ' + tblCustomers.surname AS Name,
c.street AS Street,
c.suburb AS Suburb,
c.postCode AS Postcode,
c.state AS State,
c.country AS Country,
c.phone AS [Phone No.],
c.fax AS Fax,
c.mobilePhone AS [Mobile Phone],
c.email AS [E-mail]
FROM dbo.tblCustomers c
WHERE EXISTS ( SELECT 1
FROM dbo.tblProduct_Backorder b
JOIN dbo.tblProduct p
ON b.productId = p.productId
WHERE p.skuCode = 76761
AND b.customerId = c.customerId );
If I understand your table relationships correctly, it will produce the same result while doing a lot less work.
For your original question you should also post the .net code. Also, what does "no results" mean? An empty result? A timeout? An error?
I found solution by help of xQbert's Comment. I took time and and saw how query is executed in SQL Profiler. skuCode in Table tblProduts is nvarchar(6) and in Table tblProduct_BackOrder its Integer. So, I converted into Integer for same Parameter using Convert() function.

How Can I display data coming from a database using ASP.NET & HTML

I made a query that retrieve a list of items from a database:
CREATE PROCEDURE dbo.GetSubjects(#DayComing varchar(10) , #UserId varchar(3))
AS
IF EXISTS(
SELECT Std_ID
FROM Student
WHERE Std_ID = #UserID)
BEGIN
SELECT TimeTable.Ttb_Subject
FROM Student INNER JOIN
Class ON Student.Std_Class = Class.Cls_ID INNER JOIN
TimeTable ON Class.Cls_ID = TimeTable.Ttb_Class
WHERE (TimeTable.Ttb_WeekDay = #DayComing) AND (Student.Std_ID = #UserID)
END
ELSE BEGIN
SELECT TimeTable.Ttb_Subject, TimeTable.Ttb_Class
FROM Teacher INNER JOIN
TimeTable ON Teacher.Tch_ID = TimeTable.Ttb_Teacher
WHERE (TimeTable.Ttb_WeekDay = #DayComing)
END
Now I want to show m at asp page, but I don't know how can I do it. Do you know any tutorials that can help me with this ?
Thanks.
You have several options, for example Linq2Sql, Linq2EF, ADO.NET, some other framework...
Here's a tut on how to accomplish this with ado.net:
http://www.codeproject.com/Articles/15403/Calling-Stored-procedures-in-ADO-NET
Here's how you do it with Linq 2 SQL:
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
And Linq 2 Entity framework:
http://msdn.microsoft.com/en-us/data/gg699321
You have lots of options. Have a look at the below source:
ASP.NET Data Controls
Also, you may prefer to work with an ORM technology like Entity Framework:
Working with Data (Entity Framework Tutorial)

Resources