Problem with SQL CASE sentence in C# ASP.NET

0

I'm developing a code for a project. I have an .mdb MS Access database connected to the project. I'm using the following code to show the information of a table in a GridView

conn.Open();

OleDbCommand cmd = new OleDbCommand("SELECT Fecha, DescripciĆ³n AS Mensaje, " + 
                                    "CASE WHEN Admin = true THEN 'Admin' ELSE 'Usuario' END" +
                                    "FROM Detalle WHERE IdParte = ? ORDER BY Fecha", conn);
cmd.Parameters.AddWithValue("IdParte", ddPartes.SelectedValue);

OleDbDataReader dr = cmd.ExecuteReader();

GridView1.DataSource = dr;
GridView1.DataBind();

conn.Close();

Details of the error I get:

System.Data.OleDb.OleDbException
HResult=0x80004005
Mensaje = Error de IErrorInfo.GetDescription con E_FAIL(0x80004005).
Origen =
Seguimiento de la pila:

c#
sql
asp.net
visual-studio
ms-access
asked on Stack Overflow Jul 17, 2019 by Sebastian Braga • edited Jul 17, 2019 by marc_s

1 Answer

3

You're missing a whitespace between the "END" and the "FROM". Just add it, and you should be OK:

OleDbCommand cmd = new OleDbCommand("SELECT Fecha, DescripciĆ³n AS 
    Mensaje, " + 
    "CASE WHEN Admin = true THEN 'Admin' ELSE 'Usuario' END " +
    // Space added here -----------------------------------^
    "FROM Detalle WHERE IdParte = ? ORDER BY Fecha", conn);
answered on Stack Overflow Jul 17, 2019 by Mureinik

User contributions licensed under CC BY-SA 3.0