Iman mohammadi
Iman mohammadi

Reputation: 49

sql server error = This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

I have a problem with my code.

I use this code for create a view on sql server :

SELECT
(SELECT FirstNameD + ' ' + LastNameD AS Expr1
 FROM   dbo.UsersDatas) AS UsersFullName,
 PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas AS UsersDatas_1   

and use this view in my c# application.

after I set two rows with my c# app, I get this error from Visual Studio :

System.Data.Entity.Core.EntityCommandExecutionException: 'An error occurred while reading from the store provider's data reader. See the inner exception for details.'

my c# code is:

private void SetDataGridViewDatasMethod()
{
    var Query = from MU in DataBaseDataD.VW_UsersDatasView
    select MU;
    var UsersDataD = Query.ToList();
    UsersInfoDataGridView.ItemsSource = UsersDataD;
}

and I search a lot about it on the internet but I can't find any solution could you help me fix this problem please?

Upvotes: 0

Views: 130

Answers (2)

Ian Wallace
Ian Wallace

Reputation: 1

As Shantanu says - no need for the inner query. And rather than using the "+" operator you might want to use CONCAT

SELECT CONCAT(FirstNameD, ' ', LastNameD)  AS UsersFullName,
 PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas 

Or, if you're using SQL Server 2017 you could also use CONCAT_WS

SELECT CONCAT_WS(' ', FirstNameD, LastNameD)  AS UsersFullName,
 PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas

The key difference between + and CONCAT is how they behave when one of the inputs is NULL.

Upvotes: 0

Shantanu
Shantanu

Reputation: 554

If you want fullname you can do like this, no need of inner query

 SELECT
FirstNameD + ' ' + LastNameD  AS UsersFullName,
 PhoneNumberD, EmailAddressD, UserNameD
FROM dbo.UsersDatas 

Upvotes: 4

Related Questions