0

So below I have a piece of code that I have been working on:

SqlConnection connect = new SqlConnection("Server=OMADB01;Database=PATRICK_DEV;Trusted_Connection=True;");
connect.Open();
SqlCommand command = new SqlCommand("UPDATE FILE_DATE_PROCESSED SET FILE_DATE_PROCESSED = DATE_ENTERED, DATE_ENTERED = GETDATE() SELECT top 1 FILE_DATE_PROCESSED, DATE_ENTERED FROM FILE_DATE_PROCESSED, ORDER BY DATE_ENTERED DESC ", connect);
SqlDataReader reader = null;
reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine(reader["FILE_DATE_PROCESSED"].ToString());
    Console.WriteLine(reader["DATE_ENTERED"].ToString());
}

connect.Close();

This code is supposed to write out the contents of the datatable I have in SQL, however I keep getting an error message that ORDER is incorrect syntax, however that is false because when I take that statement and put it in my query in SQL, it works. I am new to using SQL in c# so I am not quite sure how to debug with SQL statements, so if someone could please help me figure this out I would very much appreciate it!

4
  • 3
    Well, ORDER is in incorrect syntax alright, there's a comma in "FILE_DATE_PROCESSED, ORDER BY " Commented Jun 11, 2015 at 16:12
  • You should try your sql statement in sql managment studio before adding it to the C# app to eliminate most common problems. Commented Jun 11, 2015 at 16:13
  • 2
    I would prefer to see your code not so tightly coupled with the data. Changing this to use a stored procedure and moving your data to the procedure would be a good start in making your application have layers. It is hard to figure out what you are trying to do here because you are going to update all rows in your FILE_DATE_PROCESSED table to the same value. Commented Jun 11, 2015 at 16:20
  • I'd say go the stored procedure route too - it means you don't need to redploy an app if you need to change the implementation. Also it looks like you are trying to run an update which depends on the value of a select from the same table (and depends on getdate()) - in which case you shouldn't need the select at all Commented Jun 11, 2015 at 16:37

2 Answers 2

3

You cannot have two calls, update and select without having ; in between.

Sign up to request clarification or add additional context in comments.

1 Comment

The Order By is on the Select statement. He is mixing two calls. Update then Select.
2

You are mixing two statements. "Select" and "UPdate".

First try putting a ";" between the two statements.

Second: Try running Only the Select portion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.