Solved

C# syntax issue

Posted on 2014-04-14
6
251 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now