0

i trying to execute a query that link different databases OleDbDataReader, but i am getting error in query string, note that this query is correct and running successfuly in sql studio manager but in .Net i am getting this error in

OleDbDataReader reader = myCommand.ExecuteReader();

the error message:

Incorrect syntax near 'PR1'.

my code "long query string"

protected void Page_Load(object sender, EventArgs e)
{
    string strConString=System.Configuration.ConfigurationManager.ConnectionStrings["WorkflowConnStr"].ConnectionString.ToString();

    string sqlstr = "select coalesce(engdir ,'Total') [engdir]," +
    "SUM(case when a.Status = 'Delivered' then Total else 0 end) as [Delivered],"+
    "SUM(case when a.Status = 'Completed' then Total else 0 end) as [Completed],"+
    "SUM(case when a.Status = 'Pending' then Total else 0 end) as [Pending],"+
    "SUM(case when (a.Status ='Rejected') then Total Else 0 end) as [Rejected],"+
    "COUNT(*) as Total from "+
    "(select count(*) as Total, CurrentActorUID,ReferenceNo, Status,RequestedDate from tbl_ServiceTracking "+
    "where CurrentActorUID is not null and CurrentActorUID <> '' and "+
    "(Status='rejected' or  Status='Completed' or Status='Pending' or Status='Delivered' )and (ServiceNo is not null )"+
    "group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a"+
    "JOIN PR1.dbo.GetUserDetailsE AS b "+
    "ON a.CurrentActorUID = b.PERUserName"+
    "WHERE --b.DirCode=@DirCode "+
     "(CAST(a.RequestedDate AS DATETIME)>='1/1/2014' AND CAST(a.RequestedDate AS DATETIME)<='4/30/2014') "+
    "GROUP BY b.engdir "+
    "WITH ROLLUP HAVING engdir IS NOT NULL OR GROUPING(b.engdir) = 1 ORDER BY "+
    "CASE WHEN b.engdir IS NULL THEN 1 ELSE 0 END ,b.engdir ";

    OleDbConnection myConnection = new OleDbConnection(strConString);
     try {myConnection.Open();}
     catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.Message); }        

    OleDbCommand myCommand = new OleDbCommand(sqlstr, myConnection);        
    OleDbDataReader reader = myCommand.ExecuteReader();
    myCommand.ExecuteReader(CommandBehavior.CloseConnection);

    Chart1.DataBindCrossTable(
        reader,
        "Excellent",
        "Satisfied",
        "Not Satisfied",
        "Total");
}

any suggestion ?

1
  • Get used to formatting your query strings with newlines and such. It really helps spot errors like this if you can see at a glance that your string concatenations are properly delimited and whitespace is intact. Commented Apr 22, 2014 at 6:06

1 Answer 1

2

you missed a space here:

"group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a"+

should be:

"group by CurrentActorUID,Status,RequestedDate,ReferenceNo) a "+

Additionally, you can use literal '@' instead of using concatenation, something like this:

string sqlstr = @"SELECT COALESCE(engdir, 'Total') [engdir], 
                SUM(case when a.Status = 'Delivered' then Total else 0 end) as [Delivered],
                SUM(case when a.Status = 'Completed' then Total else 0 end) as [Completed],
                SUM(case when a.Status = 'Pending' then Total else 0 end) as [Pending],
                SUM(case when (a.Status ='Rejected') then Total Else 0 end) as [Rejected],
                COUNT(*) as Total from 
                (select count(*) as Total, CurrentActorUID,ReferenceNo, Status,RequestedDate from tbl_ServiceTracking
                where CurrentActorUID is not null and CurrentActorUID <> '' and 
                JOIN PR1.dbo.GetUserDetailsE AS b 
                ON a.CurrentActorUID = b.PERUserName
                WHERE --b.DirCode=@DirCode 
                (CAST(a.RequestedDate AS DATETIME)>='1/1/2014' AND CAST(a.RequestedDate AS DATETIME)<='4/30/2014') 
                GROUP BY b.engdir 
                WITH ROLLUP HAVING engdir IS NOT NULL OR GROUPING(b.engdir) = 1 ORDER BY 
                CASE WHEN b.engdir IS NULL THEN 1 ELSE 0 END ,b.engdir";

Run the query in your sql platform and from there you can easily check where the error came from. And just copy the query inside the literal string @"";

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

2 Comments

thanks for the reply, it was really a stupid mistake :D
Don't worry even Michael Jordan also made a stupid mistakes. :D

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.