Solved

C# syntax issue

Posted on 2014-04-14
6
255 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
  • 3
  • 3
6 Comments
 
LVL 34

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 34

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

806 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