Cannot get query to function

Posted on 2011-05-05
Last Modified: 2012-06-27
I am attempting to write a select query and am getting an error in my syntax

Code Follows:
 protected void ddl_Building_SelectedIndexChanged(object sender, EventArgs e)

            String strConnString = ConfigurationManager.ConnectionStrings["DetentionDWLConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConnString);

            ///Retrieve Data in Grid as a filter based on Building ID////
            string query = "SELECT (Building_ID, Building_Code, Floor_ID, Floor_Name, Shift_ID, Shift_Name, Jail_Key, Position_Title, Position_Desc, Position_Key, Composite_Key)  where Building_ID=@Building_ID";
            SqlDataAdapter adp = new SqlDataAdapter(query, con);
            DataSet ds = new DataSet();
            gv_position_existing.DataSourceID = null;
            gv_position_existing.DataSource = ds;


            ddl_floor.Items.Add(new ListItem("--Select Floor--", ""));
            ddl_shift.Items.Add(new ListItem("--Select Shift--", ""));

            ddl_floor.AppendDataBoundItems = true;
            String strQuery = "select Floor_ID, Floor_Name from CodeTable_Floor " + "where Building_ID=@Building_ID";
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@Building_ID", ddl_Building.SelectedItem.Value);
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQuery;
            cmd.Connection = con;

                ddl_floor.DataSource = cmd.ExecuteReader();
                ddl_floor.DataTextField = "Floor_Name";
                ddl_floor.DataValueField = "Floor_ID";
                if (ddl_floor.Items.Count > 1)
                    ddl_floor.Enabled = true;
                    ddl_floor.Enabled = false;
                    ddl_shift.Enabled = false;


            catch (Exception ex)
                throw ex;
                lbl_building.Text = " " + ddl_Building.SelectedItem.Text;



the error is
Incorrect syntax near ','.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ','.

Source Error:

Line 70:             SqlDataAdapter adp = new SqlDataAdapter(query, con);
Line 71:             DataSet ds = new DataSet();
Line 72:             adp.Fill(ds);
Line 73:             gv_position_existing.DataSourceID = null;
Line 74:             gv_position_existing.DataSource = ds;

Source File: C:\Legacy\LegacyIntranetSolution\Detention_DWL\DetentionWatch.aspx.cs    Line: 72

Question by:Codeaddict7423
    LVL 59

    Accepted Solution


    This query looks incorrect:
     "SELECT (Building_ID, Building_Code, Floor_ID, Floor_Name, Shift_ID, Shift_Name, Jail_Key, Position_Title, Position_Desc, Position_Key, Composite_Key)  where Building_ID=@Building_ID";

    Note you don't have a FROM clause.

    Would expect:
     "SELECT Building_ID, Building_Code, Floor_ID, Floor_Name, Shift_ID, Shift_Name, Jail_Key, Position_Title, Position_Desc, Position_Key, Composite_Key FROM [some table name] where Building_ID=@Building_ID";

    Also, I would make sure that the @Building_ID is being properly passed as a VARCHAR parameter and so that its text is not being parsed as SQL, i.e., you need the resulting query to be Building_ID = 'some id' or Building_ID = 1.  If it is a comma delimited list of building ids, that may cause the issue.

    To verify, I would write out the value of the queries to screen or check via debugging.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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

    20 Experts available now in Live!

    Get 1:1 Help Now