System.Data.OleDb.OleDbException: Undefined function 'ConcatRelated' in expression - c#

I'm working on a c# winform software, connecting to local MS Access db (.mdb)
Connection string used is Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb and it's kinda considered a requirement, Ace is not an option.
I'm trying to use the function ConcatRelated but keep encountering error:
System.Data.OleDb.OleDbException (0x80040E14): Undefined function 'ConcatRelated' in expression.
My query below:
string carColumnsAndTable = "SELECT c.Id, cust.CustomerName as InsuredName, c.CarPlate as VehicleNo, c.CarMake as Make, c.CarModel as Model, c.CarYear as VehicleYear, c.CarCc as CC, c.CarChasisNumber as Chasis, c.CarEngineNumber as EngineNumber, "
+ "ConcatRelated('d.DebtorName', 'CarDebtor cd left join Debtor d on cd.DebtorId = d.Id', 'cd.CarId = ' & [c.Id]) as Debtor "
+ "FROM ((Car as c) left join Customer as cust on c.CustomerId = cust.Id) ";
And function setup as in screenshot below:
Query works fine in ms access:
Anyone got any clue on what I should do to resolve this?

Related

Syntax error (missing operator) in query expression c# (access database)

Hello! I tried to connect 4 tables form Access in Visual Basic c# and I got "Syntax error (missing operator) in query expression", can you please help me? Thanks!
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE Nr_Contract='" + CB_Contract.Text + "'";
You have multiple tables you are joining across but have not specified which table your WHERE clause is looking up on.
string query = "Select e.Denumire_Ech, e.Descriere_Ech, e.UnitateMasura, e.Pret_Vanzare, o.Cantitate_EchOf From ECHIPAMENTE e INNER JOIN OFERTE o ON e.Cod_Echipament = o.Cod_Echipament INNER JOIN CONTRACTE c ON c.Cod_Oferta = o.Cod_Oferta INNER JOIN FACTURI f ON f.Nr_Contract = c.Nr_Contract WHERE c.Nr_Contract='" + CB_Contract.Text + "'";
Also you should watch out for SQL injection and unusual characters in the CB_Contract.Text For example if there is a ' character then the sql will fail.

Two Inner Joins in OleDb SQL query [duplicate]

This question already has answers here:
SQL multiple join statement
(3 answers)
Closed 4 years ago.
I'm trying to make an SQL query with an OleDbCommand into an Access database (.accdb).
While this command works fine (in a OleDbCommand.ExecuteReader()):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID ";
This other doesn't, and I can't understand why (all examples I see around use the exact same syntax):
string command =
"SELECT cred.* " +
"FROM TB_CREDENTIALS cred " +
"INNER JOIN TB_REL_USERS_CREDENTIALS rel ON cred.CRED_ID = rel.REL_USR_CRED_CRED_ID " +
"INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_ID ";
The exception given is the following System.Data.OleDb.OleDbException:
Syntax error (missing operator) in query expression 'cred.CRED_ID = rel.REL_USR_CRED_CRED_ID INNER JOIN TB_USERS usr ON usr.USR_ID = rel.REL_USR_CRED_USER_I' (message is cut here)
Version and details:
The database is a .accdb file created on Access 2010
The connection is created in C# with System.Data.OleDb.OleDbConnection
The connection provider is "Microsoft.ACE.OLEDB.12.0"
(This seems like a useless query, but of course I'll add a WHERE usr.SOME_FIELD = some_condition)
"For multi-table joins, you have to nest the extra joins in brackets:"
SQL multiple join statement

'Conversion failed' error is not captured while executing in C# or in stored procedure

SELECT
B.ID, R.COUNTRY_ID, R.LOOK_UP, R.VALUE
FROM
BUSINESS_TABLE B
INNER JOIN
MASTER_TABLE1 M1 on M1.MASTER_ID = B.B_ID
LEFT JOIN
MASTER_TABLE2 M2 on M2.MASTER_ID = B.B_ID
LEFT JOIN
ANOTHER_TABLE OT on OT.ID = M2.MASTER_ID
LEFT JOIN
[dbo].[TEST_REFERENCE]R ON M2.C_ID = R.LOOK_UP
WHERE
OT.ACTIVE = 1
In the above tables all the ID columns are int except TEST_REFERENCE table as below
select * from [TEST_REFERENCE]
ID REF_CODE(varchar 200) LOOK_UP(varchar 200) VALUE(varchar 200)
---- --------- ----------- ------------
1 COUNTRY 1 FRANCE
2 COUNTRY 2 GERMANY
3 USER POWER_USER ADMIN
4 USER POWER_USER MASTER
The above example query (entered by users) that I'm validating using C# and inserting into DB (if passed) for further purposes.
And below is error that I'm getting while executing the query in SSMS directly.
Msg 245, Level 16, State 1, Line 10
Conversion failed when converting the varchar value 'POWER_USER' to data type int.
But there is no error while executing the same query either by using cmd.ExecuteNonQuery() nor in stored procedure. In the stored procedure, I have the query alone inside a stored procedure and executing the procedure in C#/SSMS.
Appreciated in anyone gives solution(s).
PS: I know it is because of comparing varchar to int and I have a workaround by casting the values.
My requirement is to validate similar kind of queries using C# (by hitting DB) and insert into DB only if it pass by eliminating the erroneous queries.
NOTE: it works fine in C# (exception raised) when we have only two tables with join conditions
try this
SELECT B.ID,R.COUNTRY_ID,Convert(Varchar,R.LOOK_UP) as LookUp,R.VALUE FROM BUSINESS_TABLE B
INNER JOIN MASTER_TABLE1 M1 on M1.MASTER_ID = B.B_ID
INNER JOIN MASTER_TABLE2 M2 on M2.MASTER_ID = B.B_ID
LEFT JOIN ANOTHER_TABLE OT on OT.ID = M2.MASTER_ID
LEFT JOIN [dbo].[TEST_REFERENCE]R ON M2.C_ID = R.LOOK_UP
WHERE OT.ACTIVE = 1
The error is because you are trying to compare your M2.C_ID which is int with R.LOOK_UP which is varchar(200)
LEFT JOIN
[dbo].[TEST_REFERENCE]R ON M2.C_ID = R.LOOK_UP
According to MSDN Documentation SqlConnection.InfoMessage event occurs when SQL Server returns a warning or informational messageL
Clients that want to process warnings or informational messages sent by the server should create an SqlInfoMessageEventHandler delegate to listen to this event.
The InfoMessage event occurs when a message with a severity of 10 or less is returned by SQL Server. Messages that have a severity between 11 and 20 raise an error and messages that have a severity over 20 causes the connection to close.
Could you try this and check the messages :
//SqlConnection connection = ...;
connection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
// ... create a SqlCommand from this connection and run cmd.ExecuteNonQuery() ...
void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
{
foreach (SqlError err in args.Errors)
{
//Console.WriteLine
MessageBox.Show("The " + err.Source + " has received a severity " + err.Class + ", state " + err.State + "\n" +
"on line " + err.LineNumber + ":\n" + err.Message);
}
}

SQLException at Inner Join

I am using a C# program that calls a SQL statement that is executed on an instance of SQL Server 2008 R2. Here is the SQL Call:
SELECT TOP 1 as1.AssetTagID, as1.TagID, as1.CategoryID, as1.Description,
as1.HomeLocationID, as1.ParentAssetTagID
FROM Assets AS as1 ORDER BY ar.DateScanned DESC
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE (ar.ReadPointLocationID='Readpoint1' OR ar.ReadPointLocationID='Readpoint2')
AND as1.TagID!='000000000000000000000000';
I am getting an SQLException around INNER. The exception text is the following:
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'INNER'.
I can put the stack trace on here as well, but I felt like it would clutter the question. Here is the actual string in the C# code that I am using with the call:
"SELECT TOP 2 as1.AssetTagID, as1.TagID, " +
"as1.CategoryID, as1.Description, as1.HomeLocationID," +
"as1.ParentAssetTagID FROM Assets AS as1 ORDER BY ar.DateScanned DESC\n" +
"INNER JOIN AssetsReads AS ar ON as1.AssetTagID = ar.AssetTagID\n" +
"WHERE (ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[0] +
"' OR ar.ReadPointLocationID='" + IntegrationService.Lane2Zones[1] + "')\n" +
"AND as1.TagID!='000000000000000000000000';"
Your ORDER BY statement can't be there. Move it to the end
I'll also give the obligatory "Don't do this" speech. Concatenating SQL strings like this opens you up to SQL injection attacks. There is plenty of information about that on SO and Google so I won't go into it, but you should definitely consider making this a parameterized query.
Like he said... your order by clause was out of order :)
SELECT TOP 1 as1.AssetTagID,
as1.TagID,
as1.CategoryID,
as1.Description,
as1.HomeLocationID,
as1.ParentAssetTagID
FROM Assets AS as1
INNER JOIN AssetsReads AS ar
ON as1.AssetTagID = ar.AssetTagID
WHERE ( ar.ReadPointLocationID = 'Readpoint1'
OR ar.ReadPointLocationID = 'Readpoint2' )
AND as1.TagID != '000000000000000000000000'
ORDER BY ar.DateScanned DESC;
I'll also note that using schema qualified objects is recommended by Microsoft (http://technet.microsoft.com/en-us/library/ms190387(v=sql.105).aspx). Also you should use parenthesis around your top (value) statement.
SELECT TOP (1) [as1].[AssetTagID],
[as1].[TagID],
[as1].[CategoryID],
[as1].[Description],
[as1].[HomeLocationID],
[as1].[ParentAssetTagID]
FROM [<schema>].[Assets] AS [as1]
INNER JOIN [<schema>].[AssetsReads] AS [ar]
ON [as1].AssetTagID = [ar].[AssetTagID]
WHERE ( [ar].[ReadPointLocationID] = 'Readpoint1'
OR [ar].[ReadPointLocationID] = 'Readpoint2' )
AND cast([as1].TagID AS [INT]) != 0
ORDER BY [ar].[DateScanned] DESC;

SQL error in C#

conn.Open();
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE" +
"from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
My Query SQL runs fine in MYSQL Server but it has error in C# code Incorrect syntax near the keyword 'INNER'. hope have answer soon
You need to add space between (.PRO_ACTIVE" and "from) string concatenation.
.PRO_ACTIVE " + "from...
So your query should be:
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE"
+ " " +//explicit space
"from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
You need a space at the end of the first line of your string -- C# doesn't put line feeds in.
change it to:
String sql = "select CATEGORIES.CAT_NAME,PRODUCTS.PRO_MODEL,PRODUCTS.PRO_NAME,PRODUCTS.PRO_PRICE,PRODUCTS.PRO_IMAGE,PRODUCTS.PRO_DESCRIPTION,PRODUCTS.PRO_STATUS,PRODUCTS.PRO_ACTIVE" +
" from PRODUCTS INNER JOIN CATEGORIES on PRODUCTS.CAT_ID = CATEGORIES.CAT_ID";
A space was missing between "..PRODUCTS.PRO_ACTIVE" and "from PRODUCTS.."

Categories