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