0

I'm trying to execute this chunk of code:

public List<CustomerCaseSearch> FindCustomerInfo(string searchCondition)
{
    var Query = "SELECT  CustomerProfile.CustomerID,CustomerProfile.CustomerName,    isNull(k.CustomerSince, '''') as CustomerSince, isNull(k.CustomerStatus,'''') as CustomerStatus," +
                "CustomerProfile.CustomerType, CustomerCase.CaseID" +
                " FROM CustomerProfile INNER JOIN" +
                " CustomerCase ON CustomerProfile.CustomerID = CustomerCase.CustomerID "+ searchCondition;

    var customerCaseSearch = _CustomerCaseSearchRepository.ExecuteQueryData<CustomerCaseSearch>(@Query);
    List<CustomerCaseSearch> aList = customerCaseSearch.ToList();
    return aList;
}

And I keep getting this exception message:

SQLException was unhandled by user code. 
Incorrect syntax near 'isNull'

I need to know, what is the correct syntax of using isNull inside an ASP.NET code where I need to put SQL Query?

Please note that I am a newby to professional development so my knowledge is quite low. Kind help anticipated and will be appreciated.

3
  • 3
    The syntax seems correct, what I don't see in your statement is aliasing anything to be k. Commented Nov 26, 2013 at 13:48
  • ISNULL is a valid MSSQL function, problem is something else. Commented Nov 26, 2013 at 13:48
  • 1
    The two pair of single quotes may be problematic. Commented Nov 26, 2013 at 13:49

3 Answers 3

3

Your query comes out as:

SELECT      CustomerProfile.CustomerID,
            CustomerProfile.CustomerName,    
            isNull(k.CustomerSince, '''') as CustomerSince, 
            isNull(k.CustomerStatus,'''') as CustomerStatus,
            CustomerProfile.CustomerType, CustomerCase.CaseID 

FROM        CustomerProfile 

INNER JOIN  CustomerCase 
    ON      CustomerProfile.CustomerID = CustomerCase.CustomerID

Your calls to ISNULL refer to a table with alias k, but there is no table with that alias in your query.

It's worth noting that you've asked it to return a value that is a single apostrophe, if the value were NULL, so you may want to change that as well

Sign up to request clarification or add additional context in comments.

3 Comments

Ok I edited my code as per your instruction, but still getting the same error. :/
what is the value of searchCondition?
I need to send only either of two values: open or close. Any one of them will be sent to searchCondition. I'm sending this value from a javascript file where I defined the query string.
1

You are using table alias k that is nowhere found in your query that is why it is failing.

I reformatted it so it is easier to read, below is proper query:

SELECT cp.CustomerID, cp.CustomerName, isNull(cp.CustomerSince, '') as CustomerSince,
       isNull(cp.CustomerStatus, '') as CustomerStatus, cp.CustomerType, cc.CaseID
FROM CustomerProfile cp
INNER JOIN CustomerCase cc ON cp.CustomerID = cc.CustomerID

then you have to add your search string to the end.

Comments

0

may be you missing a space:

var Query = "SELECT  CustomerProfile.CustomerID,CustomerProfile.CustomerName,    isNull(k.CustomerSince, '''') as CustomerSince, isNull(k.CustomerStatus,'''') as CustomerStatus," +
                " CustomerProfile.CustomerType, CustomerCase.CaseID" +
                " FROM CustomerProfile INNER JOIN" +
                " CustomerCase ON CustomerProfile.CustomerID = CustomerCase.CustomerID "+ searchCondition;

4 Comments

No I still keep getting errors. I corrected it, used '', but still the exception is thrown.
So please tell me is k alias for CustomerProfile?
No. That was a mistake. I wanted to use k as alias, but later decided not to use any alias, forgot to remove that k. :) :)
please try this answer again without any alias :D. I think it work now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.