Skip to content
Advertisement

Asp.net SQL exception

I am making an ASP.net web application, and I am trying to fill a table with information from a database. However, I am getting the error:

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword ‘FROM’.

Incorrect syntax near the keyword ‘AS’.

Here is the code

And the matching grid view.

Thanks.

Advertisement

Answer

Check your string concatenation

..SELECT” +
“ion.formula

will result in

SELECTion.formula

not

SELECT ion.formula

But beyond the immediate problem, two recommendations.

  1. When trying to track down an issue like this, put in a debug breakpoint and copy the value from command.CommandText and paste it in SSMS and try to execute it. That should give you a headstart on where your problem is.
  2. Use the @ string prefix to make multiline SQL queries. As in

    SqlCommand command = new SqlCommand(@”SELECT[compound_name], cc.[nonmetal1_quantity] AS[nonMetal1_quantity], (SELECT ion.formula FROM NonMetal AS ion WHERE ion.[nonmetal_id] = cc.[nonmetal1_id]) AS[nonMetal1], cc.[nonmetal2_quantity] as [nonMetal2_quantity], (SELECT ion.formula FROM NonMetal AS ion WHERE ion.[nonmetal_id] = cc.[nonmetal2_id]) AS[nonMetal2], c.compound_id AS [compound_id] FROM Compound as c, Covalent AS cc, NonMetal AS n WHERE c.[compound_id] = cc.[compound_id] ORDER BY c.[compound_name] ASC”, conn);

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement