1

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);
            }
LP1
  • 46
  • 6
  • You are correct i will fix the typo – LP1 Dec 19 '21 at 16:48
  • 2
    Try `select state + ',' + city + ',' + street as toDisplay` then change `prodavnicaCombo.DisplayMember = "toDisplay";` Side point: you are missing `using` blocks to dispose your Sql objects – Charlieface Dec 19 '21 at 16:50
  • 3
    Please use a `using` statement for your connection, if a execption happens you leave the connection open. Also i rarely find `err.Message` useful when working with sql, use `err.ToString()` it will have a lot more debugging info. – Scott Chamberlain Dec 19 '21 at 16:52
  • @Charlieface could you be a little more specific with your answer? I tried changing the SqlCommand sc query to what you said but it doesnt work and i tried to write the storeCombo.DisplayMember as "state"+"city"+"street" but it doesn't work – LP1 Dec 19 '21 at 17:11
  • 1
    There is no tag to differentiate between WinForms, WPF, etc. Please add one. – Bent Tranberg Dec 19 '21 at 17:12
  • That's not what I said. I said to change the SQL statement to select the whole thing concatenated, with an alias `toDisplay` then change the `DisplayMember` to `toDisplay`. The problem is that `DisplayMember` will not do the concatenation for you, you have to do it manually. I also fail to understand why you are manually adding columns, the `Load` function will do that – Charlieface Dec 19 '21 at 17:14
  • @ScottChamberlain thanks for all the advice I will keep them in my mind the next time i make something like this but right now with the code i have i will just have to leave it as is because i don't have enough knowledge or will to rewrite it all in a better way lol. – LP1 Dec 19 '21 at 17:15
  • @Charlieface thank you very much your answer solved the problem. The reason why I did everything like i did it is because I am noob and I am trying to solve everything based on code i have from my professor so I can only do it in some similar way that he did it. Thanks again for your help – LP1 Dec 19 '21 at 17:25
  • 1
    I suggest you explain to your professor [how to use `using` blocks and what they do](https://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it), as they don't seem to know – Charlieface Dec 19 '21 at 19:48

1 Answers1

0

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