1

had some problem the other day and it was successfully solved as i got helped out (combobox population based on each other sql c#) the solution i came up with works like a charm:

public void City()
{

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CityName FROM City WHERE CountryName = @CountryName", conn))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                cmd.Parameters.AddWithValue("@CountryName", this.cmb_countryname.SelectedValue);

                DataTable dt = new DataTable();
                da.Fill(dt);

                DataRow dr = dt.NewRow();
                dr["CityName"] = "";
                dt.Rows.InsertAt(dr, 0);
                this.city.DisplayMember = "CityName";
                this.city.ValueMember = "CityName";
                this.city.DataSource = dt;
            }
        }
    }
}

so far, so good. Country1 selected in cmb_countryname, City1 mapped to it in cmb_cityname. as i said, no issues with it (thanks again for the user who showed this solution). but here's the twist. once i loaded both comboboxes with data, i send them to a gridview. still okay. clicking on a given row, i retrieve all columns, including country and city, to various different comboboxes. still no problem. now what i wish to happen is the following: when i change a country in this new combobox, i want to have the same behavior as it had before, i.e. it maps the country to the city. the code i have for this is the same i had for the previous one (which worked flawlessly) obviously the name of the method and the combobox is changed:

public void CityChange()
{

using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT CityName FROM City WHERE CountryName = @CountryName", conn))
    {
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            cmd.Parameters.AddWithValue("@CountryName", this.cmb_passedcountryname.SelectedValue);

            DataTable dt = new DataTable();
            da.Fill(dt);

            DataRow dr = dt.NewRow();
            dr["CityName"] = "";
            dt.Rows.InsertAt(dr, 0);
            this.passedcity.DisplayMember = "CityName";
            this.passedcity.ValueMember = "CityName";
            this.passedcity.DataSource = dt;
        }
    }
}

}

i'm facing with this error message and have been stucked with trying to solve it for more than an hour now:

"Additional information: No mapping exists from object type System.Data.DataRowView to a known managed provider native type."

to my understanding, it treats this.cmb_passedcountryname.SelectedValue like it was System.Data.DataRowView, despite is isn't. am I on the right path and if so, any ideas how should i proceed? thanks!

EDIT:

sry, forgot to mention, CityChange() is called on passedcountryname's SelectedIndexChanged property.

EDIT2:

some errors in the code, edited.

SOLUTION:

Why I get "System.Data.DataRowView" instead of real values in my Listbox?

thanks everyone, who tried to help me. cheers guys

Community
  • 1
  • 1
mihocu
  • 55
  • 7
  • Put a breakpoint on that line and also in the inspector area, write this `this.cmb_passedcountryname.SelectedValue` and post the value, please – Hackerman Dec 20 '16 at 15:21
  • according to the breakpoint, it thinks it is a DataRowView. – mihocu Dec 20 '16 at 15:29
  • this.cmb_passedcountryname.SelectedValue {System.Data.DataRowView} object {System.Data.DataRowView} – mihocu Dec 20 '16 at 15:30
  • how are you loading cmb_passedcountryname? – Stephen Dec 20 '16 at 15:54
  • @Stephen in the meantime i found the solution (see question edited) but to reply you, cmb_passedcountryname is loaded from a gridview. that was actually the problem, for some reason the the combox's value was treated as DataRowView and needed to be casted as you can see in the solution. – mihocu Dec 20 '16 at 15:59
  • @mihocu glad you solved the problem. – Stephen Dec 20 '16 at 16:01

1 Answers1

0

I think you can debug and view value in

cmb_passedcountryname.SelectedValue

May be null value