0

I'm using SqlDataReader and SqlCommand to pass the following query to my DB

query = "SELECT A.Codigo, A.Descripcion, A.Precio, " +
                    "A.Cantidad, B.Codigo AS [Código de Categoría], B.Descripcion AS [Descripción de Categoría]" +
                    " FROM BuenPrecio.DBO.Producto A" +
                    "INNER JOIN BuenPrecio.DBO.Categoria B " +
                    "ON A.CodigoCategoria = B.Codigo " +
                    "ORDER BY A.CodigoCategoria";

SqlDataReader reader;

conn = new SqlConnection(conexionString);

comando.CommandType = CommandType.Text;
comando.CommandText = query;
comando.Connection = conn;

conn.Open();
reader = comando.ExecuteReader();

(The initial declarations for conn, comando and conexionString were declared before, I'm just not including them in here.)

The query can be successfully executed in SQL Server Management Studio, and it produces the expected results.

But in my C# program, upon attempting to execute it, an SqlException is thrown by the line reader = comando.ExecuteReader():

The multi-part identifier "A.Codigo" could not be bound

(for every A column in the select, not just Codigo)

I've been reading in other similarly titled questions about the issue and most of them say it's a problem with the JOIN lacking a reference to the tables, but as you can see, both of the tables used in the JOIN are correctly referenced and aliased. Furthermore, like I said, the query executes successfully in the DBMS.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    You seem to be missing a space between `A` and `INNER JOIN`. Unless that's a typo. Not even sure that would cause the error you've given. – devNull Aug 08 '20 at 03:43
  • Maybe try and view the executed query in SQL Profiler to see if there's any formatting errors happening between the code and the DB – devNull Aug 08 '20 at 03:44
  • Good catch, that was the problem. It was running in the DBMS because I was unknowingly removing the space as I removed the extras " and + signs. –  Aug 08 '20 at 03:45
  • `AINNER` == problem and I am a little slow to the game :) good eye @devNull! – Trevor Aug 08 '20 at 03:47

2 Answers2

1

As @goldow mentioned, your SQL is malformed. There is no space before INNER and it becomes AINNER thus the error.

                " FROM BuenPrecio.DBO.Producto A" +
                "INNER JOIN BuenPrecio.DBO.Categoria B " +

To eliminate these types of problems, prefer using string literals. You just prefix your string with @ sign. It lets you use multi-lines and you can see your query more clearly (but if you need to use any double quotes " inside your query, make sure you escape with another double quote, i.e. 2 double quotes "").

query = @" 
  SELECT A.Codigo, A.Descripcion, A.Precio, 
         A.Cantidad, B.Codigo AS [Código de Categoría], B.Descripcion AS [Descripción de Categoría]
  FROM BuenPrecio.DBO.Producto A
  INNER JOIN BuenPrecio.DBO.Categoria B
            ON A.CodigoCategoria = B.Codigo 
  ORDER BY A.CodigoCategoria
";

See here more on string literals in C# Multiline string literal in C#

Bonus: Not related to your question, but never use dynamic queries where you build a SQL string with input from untrustable sources (like from a data entry form from an application). It's very hard sanitize those inputs so it's allways better to use parameterized queries. Otherwise, this can lead to SQL injection vulnerability in your application.

See here for more on parameterized queries:

How to use SqlDataReader with a parametrized query in c#?

K4M
  • 1,030
  • 3
  • 11
0

You missing spaces between the concatenation in your query, try this :

"SELECT A.Codigo, A.Descripcion, A.Precio," +
" A.Cantidad, B.Codigo AS [Código de Categoría], B.Descripcion AS [Descripción de Categoría]" +
" FROM BuenPrecio.DBO.Producto A" +
" INNER JOIN BuenPrecio.DBO.Categoria B" +
" ON A.CodigoCategoria = B.Codigo " +
" ORDER BY A.CodigoCategoria"
Goldow
  • 52
  • 5