Solved

C# syntax issue

Posted on 2014-04-14
6
257 Views
Last Modified: 2014-04-14
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
Comment
Question by:Moti Mashiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39999818
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
 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39999834
What I should do I can't change the table name this is my production database.
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39999862
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 1

Author Comment

by:Moti Mashiah
ID: 39999874
Thank you very much I just did something like this  "select * from [Order]
0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 39999879
was really helpful and save me a lot of time.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39999881
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# DateTime Format validation 4 88
Create a Dropdown list with sub group 10 42
Run software updates from the website 6 49
Returning multiple values with a stored procedure 9 31
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question