Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:34:37+00:00 2026-06-03T02:34:37+00:00

I am having an issue with the SelectedValue control. I have first created a

  • 0

I am having an issue with the SelectedValue control. I have first created a comboBox, and tied to it is the following method:

        private void Form1_Load(object sender, EventArgs e)
    {
        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 Cities ORDER BY Name ASC";
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        cboCities.ValueMember = "CityId";
        cboCities.DisplayMember = "Name";
        cboCities.DataSource = ds.Tables[0];
        cboCities.SelectedIndex = -1;

    }

Assuming this is the only code I have present in my form, the comboBox (cboCities) populates accordingly. My issue arises when I try to fill a second comboBox (cboParks) with the corresponding parks associated with that city. This method looks as follows:

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;
        }
    }

When I load up my Mobile Application, the first comboBox does not populate correctly and is in fact displaying data along the lines of: “System32.Data….”, and when selecting any of them, I am brought to the runtime error that states “There was an error parsing the query. [Token line number = 1, Token line offset = 52,Token in error = Data]”. I have been lead to believe the issue itself is from the SELECT statement here:

cmd.CommandText = "SELECT Name FROM [Parks] WHERE CityId =" + cboCities.SelectedValue + " ORDER BY Name ASC";

When I change the cboCities.SelectedValue to cboCities.SelectedItem, the comboBox cboParks populates appropriately minus the filtering (it brings back ALL the data). I can also change it to simply CityId (for testing purposes) and the SELECT statement works.

I have also tried to paramertize it as such, but am brought to an entirely different error: “No mapping exists from DbType System.Data.DataRowView to a known SqlCeType.”

                cmd.CommandText = "SELECT Name FROM [Parks] WHERE CityId = @CityId ORDER BY Name ASC";
            cmd.Parameters.AddWithValue("@CityId", cboCities.SelectedValue);

Basically, what is causing SelectedValue NOT to work and bring back the appropriate CityId ValueMember of the first method? I am pulling my hair out trying to figure this out. Also, if anyone has another method of binding selected data to comboBoxes, then please do share! I’m new to the C# world so any help is much appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:34:38+00:00Added an answer on June 3, 2026 at 2:34 am

    As from MSDN,

    The SelectedValue return the value of the member of the data source specified by the **ValueMember** property.

    You specify as ValueMember the field CityID, but in your query this field is not present.
    Therefore the SelectedValue returns the result of the ToString() method of the object.
    that happens to be a System.Data.DataRowView.

    I think you could resolve your problem simply adding that field to your query

    So change your code in this way

    using(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 CityID, Name FROM Cities ORDER BY Name ASC"; 
        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd); 
        DataSet ds = new DataSet(); 
        da.Fill(ds); 
    }        
    ....
    

    then you will be able to use parameters on the second query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having an issue here that I have tried everything I can think of but
Experts, I'm having issue when sending emails out. Currently, I have a feedback form
I'm following Railscasts Episode #165 Edit Multiple but having issue where when I go
first time I'm doing an insert from ASP.NET/C# and I'm having a little issue.
I have a primefaces dataList within a primefaces dataGrid but I'm having issue mapping
I am having issue with git pull.I have commited my changes in local repo.
I'm building a menu options, having issue in last option, The Anchor method doesn't
Im having an issue with a Stored Procedure I have written, I can call
I'm having issue assigning regex value to a string. Issue that I have is
I'm having issue with calling a method with l-value of an abstract class. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.