tags:

views:

30

answers:

1

I am getting an error message as 'Incorrect Syntax near the keyword UNION.

The target site is -

Void ProcessResults(System.Data.OleDb.OleDbHResult)

and the stacktrace is as follows:-

at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr) at System.Data.OleDb.OleDbDataReader.NextResult() at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) at MFActivityPlanning.NewStuffS.PopulateNewStuffsDataGrid()

The query is as follows:

 strSelectQuery = "SELECT LED_ID AS PK_ID, FIRST_NAME + ' ' + LAST_NAME AS NAME"
                        + " FROM M_LEADERLED INNER JOIN M_USER_DETAILS"
                        + " ON M_LEADERLED.LED_ID = M_USER_DETAILS.PK_ID"
                        + " WHERE (M_LEADERLED.LEADER_ID = " + Session["UserID"].ToString() + ""
                        + " AND M_USER_DETAILS.ACTIVE = 1 AND M_LEADERLED.START_DATE <= Getdate()"
                        + " AND M_LEADERLED.END_DATE > Getdate())"
                        + " UNION SELECT PK_ID, FIRST_NAME + ' ' + LAST_NAME AS NAME"
                        + " FROM M_USER_DETAILS WHERE PK_ID = " + Session["UserID"].ToString() + ";";



  // Query for Idea
            string selectIdea = "SELECT MWL.PK_ID AS PK_ID, 'Idea'+ ': ' + MI.TITLE AS TITLE,"
                        + " 'Idea.aspx?id=' + CONVERT(VARCHAR, MI.PK_ID) AS OBJECTURL"
                        + " FROM M_WATCHLIST MWL INNER JOIN M_IDEAS MI ON MWL.FK_OBJECT_ID = MI.PK_ID"
                        + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                        + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Idea) + ""
                        + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";
        // Query for Articles
        string selectArticle = "SELECT MWL.PK_ID AS PK_ID, 'Articles' + ': ' + MA.TITLE AS TITLE,"
                    + " 'Articles.aspx?id=' + CONVERT(VARCHAR, MA.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_ARTICLES MA ON MWL.FK_OBJECT_ID = MA.PK_ID"
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Article) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MA.IS_ACTIVE = 1";

        // Query for Tips
        string selectTips = "SELECT MWL.PK_ID AS PK_ID, 'Tips' + ': ' + MT.TITLE AS TITLE,"
                    + " 'Tips.aspx?id=' + CONVERT(VARCHAR, MT.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_TIPS MT ON MWL.FK_OBJECT_ID = MT.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Tip) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MT.IS_ACTIVE = 1";

        // Query for IWantINeed
        string selectIWantINeed = "SELECT MWL.PK_ID AS PK_ID, 'IWantINeed' + ': ' + MWN.TITLE AS TITLE,"
                    + " 'IWantINeed.aspx?id=' + CONVERT(VARCHAR, MWN.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_I_WANT_I_NEED MWN ON MWL.FK_OBJECT_ID = MWN.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.IWantINeed) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for PointCAT
        string selectPointCAT = "SELECT MWL.PK_ID AS PK_ID, 'PointCAT' + ': ' + MPS.TITLE AS TITLE,"
                    + " 'PointCAT.aspx?id=' + CONVERT(VARCHAR, MPS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_POINTCAT_SESSIONS MPS ON MWL.FK_OBJECT_ID = MPS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.PointCAT) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MPS.IS_ACTIVE = 1";

        // Query for Seminar
        string selectSeminar = "SELECT MWL.PK_ID AS PK_ID, 'Seminar' + ': ' + MS.TITLE AS TITLE,"
                    + " 'Seminar.aspx?id=' + CONVERT(VARCHAR, MS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_SEMINAR MS ON MWL.FK_OBJECT_ID = MS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Seminar) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MS.IS_ACTIVE = 1";

        // Query for CodeCAT
        string selectCodeCAT = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Code Review Feedback:"
                    + " ' + MUD1.FIRST_NAME + ' ' + MUD1.LAST_NAME AS TITLE,"
                    + " 'CodeCatFeedback.aspx?id=' + CONVERT(VARCHAR, MCS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_CODECAT_SESSIONS MCS ON MWL.FK_OBJECT_ID = MCS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " INNER JOIN M_USER_DETAILS MUD1 ON MUD1.PK_ID = MCS.REVIEW_MEMBER"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.CodeCatFeedback) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MCS.ACTIVE = 1";

        // Query for NowShowing
        string selectNowShowing = "SELECT MWL.PK_ID AS PK_ID, 'NowShowing' + ': ' + MNS.TITLE AS TITLE,"
                    + " 'NowShowing.aspx?id=' + CONVERT(VARCHAR, MNS.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_NOW_SHOWING MNS ON MWL.FK_OBJECT_ID = MNS.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.NowShowing) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MNS.IS_ACTIVE = 1";

        // Query for QA
        string selectQA = "SELECT MWL.PK_ID AS PK_ID, 'Q&A' + ': ' + MQ.TITLE AS TITLE,"
                   + " 'QA.aspx?id=' + CONVERT(VARCHAR, MQ.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_QA MQ ON MWL.FK_OBJECT_ID = MQ.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.QA) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MQ.IS_ACTIVE = 1";

        // Query for Message
        string selectMessage = "SELECT MWL.PK_ID AS PK_ID, 'Message' + ': ' + MS.SUBJECT AS TITLE,"
                   + " 'Message.aspx?id=' + CONVERT(VARCHAR, MS.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_MESSAGE MS ON MWL.FK_OBJECT_ID = MS.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Message) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MS.IS_ACTIVE = 1";

        // Query for Documents
        string selectDocuments = "SELECT MWL.PK_ID AS PK_ID, 'Documents' + ': ' + MSV.TITLE AS TITLE,"
                   + " 'Document.aspx?id=' + CONVERT(VARCHAR, MSD.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_SHARE_DOCUMENT MSD ON MWL.FK_OBJECT_ID = MSD.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " INNER JOIN M_SHARE_DOC_VERSION MSV ON MSV.FK_DOC_ID = MSD.PK_ID AND MSV.IS_ACTIVE =1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Document) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MSD.IS_ACTIVE = 1";

        // Query for Candidate
        string selectCandidate = "SELECT MWL.PK_ID AS PK_ID, 'Candidate' + ': ' + MC.CAND_NAME AS TITLE,"
                   + " 'Candidates.aspx?id=' + CONVERT(VARCHAR, MC.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_CANDIDATE MC ON MWL.FK_OBJECT_ID = MC.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Candidate) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for Prospect
        string selectProspect = "SELECT MWL.PK_ID AS PK_ID, 'Prospect' + ': ' + MP.PROSPECT_NAME AS TITLE,"
                   + " 'Prospect2.aspx?id=' + CONVERT(VARCHAR, MP.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN M_PROSPECT MP ON MWL.FK_OBJECT_ID = MP.PK_ID "
                   + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.Prospect) + ""
                   + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1";

        // Query for ToolCAT
        string selectToolCAT = "SELECT MWL.PK_ID AS PK_ID, 'ToolCAT' + ': ' + MT.TOOL_NAME AS TITLE,"
                    + " 'Catalyst/ToolCAT.aspx?id=' + CONVERT(VARCHAR, MT.PK_ID) AS OBJECTURL"
                    + " FROM M_WATCHLIST MWL INNER JOIN M_TOOLCAT MT ON MWL.FK_OBJECT_ID = MT.PK_ID "
                    + " INNER JOIN M_USER_DETAILS MUD ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                    + " WHERE MWL.OBJECT_TYPE = " + Convert.ToInt32(WatchType.ToolCAT) + ""
                    + " AND MWL.FK_USER_ID = " + userId + " AND MWL.IS_ACTIVE = 1 AND MT.IS_ACTIVE = 1";

string selectLearningMatter = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Learning Matter: ' + KMLM.TITLE AS TITLE,"
                        + " 'KLMS/LearningMatterView.aspx?id=' + CONVERT(VARCHAR, KMLM.PK_ID) AS OBJECTURL"
                        + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_LEARNING_MATTER KMLM"
                        + " ON MWL.FK_OBJECT_ID = KMLM.PK_ID INNER JOIN M_USER_DETAILS MUD"
                        + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                        + " WHERE MWL.OBJECT_TYPE = 505 AND MWL.FK_USER_ID = " + userId + " AND"
                        + " MWL.IS_ACTIVE = 1 AND KMLM.IS_ACTIVE = 1";
        // Query for Questions
        string selectQuestions = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Questions: ' + CONVERT(VARCHAR(400),"
                   + " [QUESTION]) AS [TITLE],"
                   + " 'KLMS/Question.aspx?id=' + CONVERT(VARCHAR, KMQ.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_QUESTIONS KMQ"
                   + " ON MWL.FK_OBJECT_ID = KMQ.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 510 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMQ.IS_ACTIVE = 1";

        // Query for Test
        string selectTest = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Tests: ' + KMT.TITLE AS TITLE,"
                   + " 'KLMS/Test.aspx?id=' + CONVERT(VARCHAR, KMT.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_TESTS KMT"
                   + " ON MWL.FK_OBJECT_ID = KMT.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 515 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMT.IS_ACTIVE = 1";

        // Query for Course
        string selectCourse = "SELECT DISTINCT MWL.PK_ID AS PK_ID, 'Courses: ' + KMC.TITLE AS TITLE,"
                   + " 'KLMS/Course.aspx?id=' + CONVERT(VARCHAR, KMC.PK_ID) AS OBJECTURL"
                   + " FROM M_WATCHLIST MWL INNER JOIN KLMS_M_COURSES KMC"
                   + " ON MWL.FK_OBJECT_ID = KMC.PK_ID INNER JOIN M_USER_DETAILS MUD"
                   + " ON MUD.PK_ID = MWL.FK_USER_ID AND MUD.ACTIVE = 1"
                   + " WHERE MWL.OBJECT_TYPE = 500 AND MWL.FK_USER_ID = " + userId + " AND"
                   + " MWL.IS_ACTIVE = 1 AND KMC.IS_ACTIVE = 1";

        //************************************************************************************************

        string orderQuery = " ORDER BY " + GridViewSortExpression + " " + GridViewSortDirection;

        //For All
        if (ddlTypes.SelectedValue == "10")
        {
            selectStatement = selectArticle + " UNION " + selectTips + " UNION " + selectIdea
                    + " UNION " + selectIWantINeed + " UNION " + selectPointCAT + " UNION " + selectSeminar
                    + " UNION " + selectCodeCAT + " UNION " + selectNowShowing + " UNION " + selectQA
                    + " UNION " + selectMessage + " UNION " + selectDocuments + " UNION " + selectCandidate
                    + " UNION " + selectProspect + " UNION " + selectToolCAT;

Can anyone please help me....

A: 

Pretty large query. I would add a breakpoint(F9) inside the if and watch the selectStatement; copy the query from there and into my sql editor, and then run/fix it from there.

It seems you are missing some space, or column in the UNION statements.

KMan