• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

C# syntax issue

Hi Guys ,

I'm trying to fill up some of my combobox with the database and I'm getting error "Incorrect syntax near the keyword'Order'.

Please review my code:

void Fillcombo()
        {
            string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=sa;Password=45Tadabase";
            string Query = "select * from rmsmasterdbtest.dbo.Order  ;";
            SqlConnection Myconn = new SqlConnection(conn);
            SqlCommand cmdDataBase = new SqlCommand(Query, Myconn);
            SqlDataReader Reader;
            try
            {
                Myconn.Open();
                Reader = cmdDataBase.ExecuteReader();
                while (Reader.Read())
                {
                    string ids = Reader.GetInt32(Reader.GetOrdinal("ID")).ToString();
                    comboBox1.Items.Add(ids);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Myconn.Close();
            }

Open in new window

0
Moti Mashiah
Asked:
Moti Mashiah
  • 3
  • 3
1 Solution
 
Dan CraciunIT ConsultantCommented:
ORDER is a reserved word. Do not use it as the name of a table.
http://msdn.microsoft.com/en-us/library/ms189822%28v=sql.105%29.aspx

HTH,
Dan
0
 
Moti MashiahAuthor Commented:
What I should do I can't change the table name this is my production database.
0
 
Dan CraciunIT ConsultantCommented:
Not familiar with T-SQL, but you could try to put the names between [] to force the parser to consider ORDER to be a table name, not keyword.

Something like:
string Query = "select * from [rmsmasterdbtest.dbo.Order];";
or
string Query = "select * from rmsmasterdbtest.dbo.[Order];";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Moti MashiahAuthor Commented:
Thank you very much I just did something like this  "select * from [Order]
0
 
Moti MashiahAuthor Commented:
was really helpful and save me a lot of time.
0
 
Dan CraciunIT ConsultantCommented:
Glad I could help!

PS: now you can go back at your previous questions and use the order table instead of the workarounds you used.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now