0

My code seems correct. But when I add the Group keyword in the query it produces a message:

Incorrect syntax near the keyword 'Group'

but when I remove the Group keyword the program runs successfully.

private void CSRMaintReviewer_Load(object sender, EventArgs e)
    {
        this.MaintReviewertbl.DataSource = null;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["csrapps"].ConnectionString);

        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select " +
                          "EmailID_Reviewer, " +
                          "Reviewer_Name, " +
                          "Reviewer_Email, " +
                          "EmailID_TeamLead, " +
                          "TeamLead_Name, " +
                          "TeamLead_Email, " +
                          "Site, " +
                          "Business_Unit, " +
                          "Group, " +
                          "Station, " +
                          "Pkg_Department, " +
                          "Region, " +
                          "Account, " +
                          "Key_Field, " +
                          "EmailID_SiteManager, " +
                          "SiteManager_Name, " +
                          "SiteManager_Email, " +
                          "EmailID_SiteDirector, " +
                          "SiteDirector_Name, " +
                          "SiteDirector_Email, " +
                          "EmailID_President, " +
                          "President_Name, " +
                          "President_Email, " +
                          "Customer, " +
                          "Flag, " +
                          "CreatedBy, " +
                          "DateCreated, " +
                          "LastUpdatedBy, " +
                          "DateUpdated " +
                          "from dbo.tblCSRMaintReviewer ";

        try
        {
            SqlDataReader reader = null;
            reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    MaintReviewer reviewer = new MaintReviewer();
                    reviewer.EmailIDReviewer = reader["EmailID_Reviewer"].ToString();
                    reviewer.ReviewerName = reader["Reviewer_Name"].ToString();
                    reviewer.ReviewerEmail = reader["Reviewer_Email"].ToString();
                    reviewer.EmailIDTeamLead = reader["EmailID_TeamLead"].ToString();
                    reviewer.TeamLeadName = reader["TeamLead_Name"].ToString();
                    reviewer.TeamLeadEmail = reader["TeamLead_Email"].ToString();
                    reviewer.Site = reader["Site"].ToString();
                    reviewer.BusinessUnit = reader["Business_Unit"].ToString();
                    reviewer.Group = reader["Group"].ToString();
                    reviewer.Station = reader["Station"].ToString();
                    reviewer.PKGDepartment = reader["Pkg_Department"].ToString();
                    reviewer.Region = reader["Region"].ToString();
                    reviewer.Account = reader["Account"].ToString();
                    reviewer.KeyField = reader["Key_Field"].ToString();
                    reviewer.EmailIDSiteManager = reader["EmailID_SiteManager"].ToString();
                    reviewer.SiteManagerName = reader["SiteManager_Name"].ToString();
                    reviewer.SiteManagerEmail = reader["SiteManager_Email"].ToString();
                    reviewer.EmailIDSiteDirector = reader["EmailID_SiteDirector"].ToString();
                    reviewer.SiteDirectorName = reader["SiteDirector_Name"].ToString();
                    reviewer.SiteDirectorEmail = reader["SiteDirector_Email"].ToString();
                    reviewer.EmailIDPresident = reader["EmailID_President"].ToString();
                    reviewer.PresidentName = reader["President_Name"].ToString();
                    reviewer.PresidentEmail = reader["President_Email"].ToString();
                    reviewer.Customer = reader["Customer"].ToString();
                    reviewer.Flag = reader["Flag"].ToString();
                    reviewer.CreatedBy = reader["CreatedBy"].ToString();
                    reviewer.DateCreated = reader["DateCreated"].ToString();
                    reviewer.LastUpdatedBy = reader["LastUpdatedBy"].ToString();
                    reviewer.DateUpdated = reader["DateUpdated"].ToString();
                    string[] row = { reviewer.EmailIDReviewer, reviewer.ReviewerName, reviewer.ReviewerEmail, reviewer.EmailIDTeamLead, reviewer.TeamLeadName,
                                     reviewer.TeamLeadEmail, reviewer.Site, reviewer.BusinessUnit, reviewer.Group,  reviewer.Station, reviewer.PKGDepartment,
                                     reviewer.Region, reviewer.Account, reviewer.KeyField, reviewer.EmailIDSiteManager, reviewer.SiteManagerName,
                                     reviewer.SiteManagerEmail, reviewer.EmailIDSiteDirector, reviewer.SiteDirectorName, reviewer.SiteDirectorEmail, reviewer.EmailIDPresident,
                                     reviewer.PresidentName, reviewer.PresidentEmail, reviewer.Customer, reviewer.Flag, reviewer.CreatedBy,
                                     reviewer.DateCreated, reviewer.LastUpdatedBy, reviewer.DateUpdated };
                    reviewers.Add(reviewer);
                }
                MaintReviewertbl.DataSource = reviewers;
                MaintReviewertbl.Refresh();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
            conn.Dispose();
            cmd.Dispose();
        }
    }
5
  • are you sure you're using right syntax of GROUP BY clause? I'm sure you'll have syntax error if you add TRUMPET to your query. Commented Aug 3, 2017 at 7:06
  • Just use bracket enclosure [Group] for Group column (which conflicts with GROUP BY keyword clause) & the query will run fine. Commented Aug 3, 2017 at 7:09
  • You can add a keyword as a field name by putting the name between [] in sql. ex.: "[group]" Commented Aug 3, 2017 at 7:09
  • "Group" is a reserved keyword. You can find complete answer here Commented Aug 3, 2017 at 7:10
  • side note: SqlDataReader also implements IDisposable. You might also want to check out ADONETHelper. It could make your code simpler. Commented Aug 3, 2017 at 8:13

2 Answers 2

5

Its giving you an error because Group is a keyword(words that have a special meaning in SQL like Select and from). This Group is conflicting with Group By and you are using it as a column name. You should change your column name in the table to something like Groupname or GroupType anything that is not a keyword in SQL. This will solve the error.

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

7 Comments

Yes, Group is one of the column name in my table. But I can't change the name of the column in the table because the database that I'm using is not accessible by any users including me.
cmd.CommandText = "select " + "EmailID_Reviewer, " + "[Group], " + "from dbo.tblCSRMaintReviewer ";
I just did that a moment ago. But the message box produces the " [Group] " message.
try this cmd.CommandText = "select " + "A.EmailID_Reviewer, " + "A.Group, " + "from dbo.tblCSRMaintReviewer A ";
Hate to say this but the second suggestion still produces the same output.
|
2

Looks like you are having a column named Group, but it's a keyword So I suggest you to change the column name(if it will not need severe coding changes) or else simply enclose them in a pair of [], like this [Group]. Keep in mind its not a good practice to give such keywords for other purposes, they are already reserved for some other purposes

4 Comments

How can I do that without changing the column name in the table?
@HoneyBadger: use like this: ... "Business_Unit, " + "[Group], " + "Station, " +...
I just did it. But it produces the same output.
What is happening now? are you getting the same error or something else

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.