Question

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 ?

Was it helpful?

Solution

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 @"";

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top