none
"There was an error parsing the query. [Token line number = 1, Token line offset = 52,Token in error = Data]"

    Question

  • I am trying to select a set of data for a ComboBox based on the selection of the previous ComboBox. I have been lead to believe it is the SELECT statement in the second method, but I cannot figure out why it isn't working. When I run the application I receive this error: "There was an error parsing the query. [Token line number = 1, Token line offset = 52,Token in error = Data]" I have attempted using Parameter.AddWithValue, cmd.Parameters.Add, and also setting the value as a string to no avail. Would anyone mind teaching me how to correctly resolve this? Thank you.

    The setup of the database is as follows:

    Cities
    - CityId (PK, int, not null)
    - Name (nchar(20), null)
    - rowguid (uniqueidentifier, not null)

    Parks
    - ParkId (PK, int, not null)
    - CityId (FK, int, not null)
    - Name (nchar(30), null)
    - rowguid (uniqueidentifier)

    Here is the method:

    private void cboCities_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCities.SelectedIndex > -1)
            {
                SqlCeConnection cn = new SqlCeConnection(@"Data Source = \Program Files\ParkSurvey\ParkSurvey.sdf; Persist Security Info = False; Password = *");
                cn.Open();
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = "SELECT Name FROM [Parks] WHERE CityId =" + cboCities.SelectedValue + " ORDER BY Name ASC";
                SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                cn.Close();
                cboParks.ValueMember = "ParkId";
                cboParks.DisplayMember = "Name";
                cboParks.DataSource = ds.Tables[0];
                cboParks.SelectedIndex = -1;
            }




    • Edited by Drinan Monday, April 30, 2012 7:50 PM
    Monday, April 30, 2012 7:12 PM

All replies

  • cmd.CommandText = "SELECT Name FROM Parks WHERE (CityId = '" + cboCities.SelectedValue + "') ORDER BY Name ASC";

    //or this should work

    cmd.CommandText = "SELECT Name FROM Parks WHERE CityId = '" + cboCities.SelectedValue + "' ORDER BY Name ASC";
    SELECT SUM([Units In Stock]) AS [Units Remaining]
    FROM Products
    WHERE (Discontinued = 'True')

    This is correct:

    SELECT * FROM Persons WHERE FirstName='Tove'

    This is wrong:

    SELECT * FROM Persons WHERE FirstName=Tove

    http://www.w3schools.com/sql/sql_where.asp

    Tuesday, May 01, 2012 4:09 AM
  • CityId is an integer so single quotes are not necessary. 
    Tuesday, May 01, 2012 6:04 AM
  •  cmd.CommandText = "SELECT Name FROM [Parks] WHERE CityId =" + cboCities.SelectedValue.ToString() + " ORDER BY Name ASC";
    Try this please.
    Wednesday, May 02, 2012 12:23 AM
  • Sadly I am brought to the same issue. :(
    Thursday, May 03, 2012 10:20 PM
  • That is strange, I have a table running on my ARM9 and tested it, works fine here.

    SqlCeConnection cn = new SqlCeConnection(ConnectString());
                cn.Open();
                SqlCeCommand cmd = cn.CreateCommand();
                cmd.CommandText = " SELECT Device02 FROM [TimerTable] WHERE Time=" + comboBox1.SelectedItem.ToString() +" ORDER BY Device02 ASC";
                SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
                da.Fill(timersDataSet);
                cn.Close();
                comboBox2.ValueMember = "Device02";
                comboBox2.DisplayMember = "Time";
                comboBox2.DataSource = timersDataSet.Tables[0];
                comboBox2.SelectedIndex = -1;

    Friday, May 04, 2012 3:51 AM