Solved

C# syntax issue

Posted on 2014-04-14
6
258 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
Independent Software Vendors: 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!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This videā€¦
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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