Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an aspx page to display reports on button click.
When I run this for first time,it displays the report properly but from second time onwards, it gives the following error message :
' Server Error in / application'
Incorrect syntax near the keyword 'JOIN'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Here is the query:
SQL
sql = "SELECT   tblIssues.ISID, ";
        sql += "        tblIssues.IssueNumber, ";
        sql += "        offices.Details AS Office, ";
        sql += "        lstIssueStatus.Details AS IssueStatus, ";
        sql += "        CONVERT(NVARCHAR,tblIssues.IssueDate,105) AS IssueDate, ";
        sql += "        CONVERT(NVARCHAR,tblIssues.RaisedDate,105) AS RaisedDate, ";
        sql += "        lstIssueType.Details AS IssueType, ";
        sql += "        raised_users.Firstname + ' ' + raised_users.Lastname AS RaisedBy, ";
        sql += "        lstLanguage.Details AS SrcLang, ";
        
        //sql += "        lstClientNames.Details AS Client, ";
        sql += "        CASE lstClientNames.Details ";
        sql += "        WHEN 'Others' THEN ISNULL(tblissues_OtherClient.OtherClientName, lstClientNames.Details) ";
        sql += "        ELSE lstClientNames.Details ";
        sql += "        END AS Client, ";
        
        sql += "        ImpactonAccount.Details AS ImpactonAccount, ";
        sql += "        tblIssues.Impact_Financial, ";
        sql += "        assigned_users.Firstname + ' ' + assigned_users.Lastname AS AssignedTo, ";
        sql += "        tgtoffices.Details AS TgtOffice, ";
        sql += "        tblIssues.Vendor, ";
        sql += "        Category.Details AS Category, ";
        sql += "        SubCategory.Details AS SubCategory, ";
        sql += "        CONVERT(NVARCHAR,tblIssues.TargetDate,105) AS TargetDate, ";

        sql += "        (dbo.f_GetTargetLangs(tblIssues.ISID)) AS TgtLangs, ";          // 25
        sql += "        (dbo.f_GetDepartments(tblIssues.ISID)) AS Departments, ";        // 26
        
        sql += "        tblIssues.ProjectNumber, ";
        sql += "        tblIssues.JobNumber, ";
        sql += "        tblIssues.IssueDesc, ";
        sql += "        RootCause.Details AS RootCause, ";
        sql += "        tblIssues.RootCauseDetail, ";
        sql += "        CONVERT(NVARCHAR,tblIssues.DateClosedbyQM,105) AS DateClosed, ";
        sql += "        tblIssues.ActFixImmediate, ";
        sql += "        ResponseAssessment.Details As ResAssess, ";
        //sql += "        tblResolutionLogs.ResLogDetail As ResponseLog, ";
        sql += "        (SELECT TOP 1 ";
        sql += "        [ActionDate] ";
        sql += "        FROM [CARs].[dbo].[tblResolutionLogs] ";
        sql += "        WHERE ISID = tblIssues.ISID ";
        sql += "        ORDER BY [ActionDate] DESC) AS DateLog, "; // Added to get Date in Res Log(s).
        sql += "        (SELECT TOP 1 ";
        sql += "        [ResLogDetail] ";
        sql += "        FROM [CARs].[dbo].[tblResolutionLogs] ";
        sql += "        WHERE ISID = tblIssues.ISID ";
        sql += "        ORDER BY [ActionDate] DESC) AS ResponseLog, ";
        sql += "        tblIssues.VerificationByQM ";

        sql += "FROM	tblIssues WITH(NOLOCK) ";
        sql += "LEFT JOIN	lstOfficeName offices WITH(NOLOCK) ";
        sql += "ON		tblIssues.OfficeID = offices.NID ";
        sql += "LEFT JOIN	lstIssueStatus WITH(NOLOCK) ";
        sql += "ON		tblIssues.IssueStatus = lstIssueStatus.NID ";
        sql += "LEFT JOIN	lstIssueType WITH(NOLOCK) ";
        sql += "ON		tblIssues.IssueTypeID = lstIssueType.NID ";
        sql += "LEFT JOIN	tblUsers raised_users WITH(NOLOCK) ";
        sql += "ON		tblIssues.RaisedByID = raised_users.UID ";
        sql += "LEFT JOIN	lstLanguage WITH(NOLOCK) ";
        sql += "ON		tblIssues.SrcLangID = lstLanguage.NID ";
        sql += "LEFT JOIN	lstClientNames WITH(NOLOCK) ";
        sql += "ON		tblIssues.ClientID = lstClientNames.NID ";
        sql += "LEFT JOIN	ImpactonAccount WITH(NOLOCK) ";
        sql += "ON		tblIssues.ImpactonAccountID = ImpactonAccount.NID ";
        sql += "LEFT JOIN	tblUsers assigned_users WITH(NOLOCK) ";
        sql += "ON		tblIssues.AssignedTo = assigned_users.UID ";
        sql += "LEFT JOIN	lstOfficeName tgtoffices WITH(NOLOCK) ";
        sql += "ON		tblIssues.TargetOfficeID = tgtoffices.NID ";
        sql += "LEFT JOIN	Category WITH(NOLOCK) ";
        sql += "ON		tblIssues.CategoryID = Category.NID ";
        sql += "LEFT JOIN	RootCause WITH(NOLOCK) ";
        sql += "ON		tblIssues.RootCauseID = RootCause.NID ";
        sql += "LEFT JOIN	ResponseAssessment WITH(NOLOCK) ";
        sql += "ON		tblIssues.ResAssessID = ResponseAssessment.NID ";
        sql += "LEFT JOIN	SubCategory WITH(NOLOCK) ";
        sql += "ON		tblIssues.SubCategoryID = SubCategory.NID ";
        sql += "LEFT JOIN	tblIssues_OtherClient WITH(NOLOCK) ";
        sql += "ON		tblIssues.ISID = tblIssues_OtherClient.ISID ";
        sql += foo.filter;
        sql += "ORDER BY tblIssues.IssueNumber";

And foo.filter is taken as
foo.filter += " JOIN	tblIssueDepartments WITH(NOLOCK) ";
              foo.filter += "ON tblIssueDepartments.ISID = tblIssues.ISID ";
              foo.filter += " JOIN Department WITH(NOLOCK) ";
              foo.filter += "ON Department.NID = tblIssueDepartments.DepartmentID ";
              foo.filter += " WHERE	Department.Details LIKE '" + Global.filterDepartmentName(user.Department, "Translation") + "%' ";

Please help as this is very urgent.
Can anybody tell me why I am getting this error message ??? I am using VS 2010 and SQL Server 2008.
Posted
Updated 5-Mar-12 1:13am
v3
Comments
Prasad_Kulkarni 5-Mar-12 7:14am    
added 'pre' tags
Prasad_Kulkarni 5-Mar-12 7:15am    
foo.filter += " JOIN tblIssueDepartments WITH(NOLOCK) ";
foo.filter += "ON tblIssueDepartments.ISID = tblIssues.ISID ";
foo.filter += " JOIN Department WITH(NOLOCK) ";
foo.filter += "ON Department.NID = tblIssueDepartments.DepartmentID ";
foo.filter += " WHERE Department.Details LIKE '" + Global.filterDepartmentName(user.Department, "Translation") + "%' ";

can u please give explanation of this query??
Member 8591985 5-Mar-12 7:42am    
the above query is used to get report based on user's department.
For eg.
if user.department= engineering then the query will search where department is engineering.
if user.department= sales then the query will search where department is sales.

1 solution

Why are you even trying to concatenate such a large block of strings? The first choice for this should be to use a stored procedure. If you can do that then use a string resource for the bulk of your sql statement. If you absolutely must concatenate this string al least use a StringBuilder.

The first option will allow you to debug this better and see where you may have mistyped or not added the appropriate spaces. The second will keep you from having to concatenate some much and make the code more maintainable. The third will allow for better memory utilization and better perform application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900