I am building a form application for ordering phones. I have a drop down list which needs to have addresses of different stores. The address is stored in a MSSQL database where state,city,street and zip code are different columns in the table store. I am having trouble with getting the drop down list to show state,city,street instead of wanted data i get System.DataRowView in the drop down list as a option. I tried just requiring city or state and it works and shows the correct data.
Is there a way of getting all three informations(state,city,street) or should i just alter the sql table to contain this data in one column?
Here is the code:
try
{
SqlConnection connection = getConnection();
connection.Open();
SqlCommand sc = new SqlCommand("select state,city,street,sotreID from store", connection);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("street", typeof(string));
dt.Columns.Add("storeID", typeof(int));
dt.Load(reader);
prodavnicaCombo.ValueMember = "storeID";
prodavnicaCombo.DisplayMember = "state,city,street"; //the troublsome line is here
prodavnicaCombo.DataSource = dt;
connection.Close();
}
catch (Exception err)
{
MessageBox.Show("Exception: " + err.Message);
}
Only one column can be used as in ValueMember & DisplayMember property. But here is wayaround you can use, instead of altering you database just concatenate your display member columns in your query and use it
try
{
SqlConnection connection = getConnection();
connection.Open();
SqlCommand sc = new SqlCommand("select (state + ' ' city + ' ' + street) as displayMember,sotreID from store", connection);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("street", typeof(string));
dt.Columns.Add("storeID", typeof(int));
dt.Load(reader);
prodavnicaCombo.ValueMember = "storeID";
prodavnicaCombo.DisplayMember = "displayMember"; //the troublsome line is here
prodavnicaCombo.DataSource = dt;
connection.Close();
}
catch (Exception err)
{
MessageBox.Show("Exception: " + err.Message);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.