0

I need help with an sql statement.

I am managing a real estate database. I have a table Properties and multiple tables Bedrooms, Bathrooms etc.

I have a form in which my agent can see in a listbox all the properties, selects the one he likes and it will display the right informations in all the combobox

Each criteria is shown in a combobox. So if you show Property 1 you see : Bedrooms: 1bedroom

Right now my sql statement is:

   "SELECT AreaSize.AreaSizeID, AreaSize.DescriptionSurface, Bathrooms.BathroomID, Bathrooms.DescriptionBathroom, Cities.CityID, Cities.DescriptionCities, Prices.PriceID, Prices.DescriptionPrices, Properties.*, Rooms.RoomID, Rooms.DescriptionRooms, Types.TypeID, Types.DescriptionType, Users.* FROM Users INNER JOIN (Types INNER JOIN (Rooms INNER JOIN (Prices INNER JOIN (Cities INNER JOIN (Bathrooms INNER JOIN (AreaSize INNER JOIN Properties ON AreaSize.AreaSizeID = Properties.AreaSize) ON Bathrooms.BathroomID = Properties.Bathrooms) ON Cities.CityID = Properties.City) ON Prices.PriceID = Properties.Price) ON Rooms.RoomID = Properties.Rooms) ON Types.TypeID = Properties.PropertyType) ON Users.UserID = Properties.AgentID WHERE Users.email =@email";


How I display in a combobox:

cboBedrooms.Text = tbListing.Rows[idx]["DescriptionRooms"].ToString();

The problem is that right now it only shows the value of the property selected and not all the other values.

if i am in property 2, combobox for bedrooms shows only: 2 bedrooms
­­­­ it should show: 2 bedrooms, than you click on the arrow and shows 1 bedroom

+-----------------------------------------+
|    Properties                           |
+------------+------------+---------------+
| PropertyID | NbBedrooms | AgentID...    |
| 1          | 1          |  2...         |
| 2          | 2          |   1...        |
+------------+------------+------------+
+-------------------------+
|    Bedrooms             |
+------------+------------+
| PropertyID | NbBedrooms |
| 1          | 1bedroom   |
| 2          | 2bedrooms  |
+------------+------------+

1 Answer 1

1

Your issue is that you are only setting the text of the ComboBox and not the actual items collection from which you can select. To achieve this simply loop through your DataTable object and add a new item to the ComboBox for each option.

foreach (DataRow row in tbListing.Rows)
{
    cboBedrooms.Items.Add(row["DescriptionRooms"].ToString();
}

Then once you've filled the Items collection you can set the SelectedItem property of the ComboBox equal to the currently set value for the property to show your user what the value is before they potentially make a change.

So if your loop were to go through and fill your ComboBox with values A, B, C, D and you wanted to set the selected value to B you would simply do:

cboBedrooms.SelectedItem = "B";

To go an extra step farther you'll probably want to write back to your database the new value your user selected if they want to make a change. To grab the selected value of the ComboBox you can simply use the same SelectedItem property with .ToString() suffixing it in your case.

Note: The SelectedItem property gets and sets a type of object. Seeing as you're only using strings it is okay to do a simple .ToString() to fetch the selected value but in the case you would want to use more complex objects you would have to first cast to that object and then retrieve the value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.