Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on hotel system by C# and sql the main screen has FlowLayoutPanel filled with rooms the rooms takes labels shape and i have floors takes buttons shape, i have 2 tables the 1st called Rooms and the 2nd called "Floors", let's say i have rooms in floor 1 ,floor 2,floor 3 ,etc
the point is how to select the rooms which are in the floor number 1 or 2 or 3
by clicking the floor buttons
i tried to code it but it doesn't work

This is my code:-

C#
private void FE1_Method()
        {
            foreach (DataRow C3 in this.dB1_PureEyezDataSet.Tables["Floor"].Rows)
            {
                try
                {
                    Button B2 = new Button();
                    B2.FlatStyle = FlatStyle.Standard;
                    B2.Width = 60;
                    B2.Height = 22;
                    B2.BackColor = Color.LightSteelBlue;
                    B2.AutoSize = true;
                    B2.Dock = DockStyle.Top;
                    B2.Anchor = AnchorStyles.None;
                    B2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                    B2.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
                    FLP2.Controls.Add(B2);
                    B2.Text = C3["FLOName"].ToString();
                    //---------------------------------------------
                    B2.Click +=
                    delegate(object sender, EventArgs e)
                    {
                        //i should write a simple code here to get the rooms
                    };
                }
                catch (Exception)
                {
                }
            }
        }

FLP2 is a FlowLayoutPanel
i just need the code which can make me filter the rooms by floor number

i hope that my problem is so clear

snake
Posted
Updated 24-Jun-11 16:07pm
v2

1 solution

This[^] page from MSDN has examples.

You would need a filter expression something like
C#
string filterExp = "Floor = 1";
string sortExp = "";
DataRow[] drarray;
drarray = dataSet1.Rooms.Select(filterExp, sortExp, DataViewRowState.CurrentRows);

(from the first example on the linked page).

NOTE that you get an Array of DataRows which you will have to decide how to deal with. Either use the Array as it is or add them into another DataTable.

Hope this helps. :)

[Edit]
Incidentally you appear to be coding yourself into a corner because of the way you keep adding features/requirements.
This is how commercial projects become unmaintainable and eventually fail.

I have pointed out a way for you to achieve what you have asked for, given your current code. It is not, however, the best way to go about this.

The best way would be for a floor to be indicated/selected by the user and then a Parameterized Query (google that for more info) should be used to only retrieve the Room data for that floor.

If you are not too far along, you should really stop and list out all the functionality that you want your application to have and then start over, bearing all of those in mind as you go.
[/Edit]
 
Share this answer
 
v2
Comments
RaviRanjanKr 31-Mar-11 0:42am    
Nice Answer Henry! My 5 :)
snake1 31-Mar-11 7:50am    
hello

i think u didn't get it i want the user to be able to add floors to the hotel also if the floor is added ,it will appear as button shape if he clicked on that button the rooms which belongs to this floor is gonna be appear on the main screen as labels
Henry Minute 31-Mar-11 11:09am    
No. I don't think you get it.

In the code I posted, (string filterExp = "Floor = 1";) the '1' comes from the button for floor 1. If it was the button for floor 2 then it would be string filterExp = "Floor = 2";. Following the rest of the example from the link would then filter your DataSet by the Floor number.

If you do not have a 'Floor' column in your rooms table, then from what you have told us so far, I would suggest that a redesign of your database might be in order. Or, explain the structure of your database in more detail so that a more suitable answer can be proposed.
snake1 1-Apr-11 10:20am    
hello Henry Minute
Thanks very much for the help
but there's a little question here i have retrieve the rooms which belongs to the floor 1 but i can't make the status of them like i did before
explaining more:-i have just retrieved the new rooms as labels which belongs to floor number 1 but i still can't make the status if it's 1 it's gonna be limegreen if it's 2 it's gonna be Red , etc like what u have explained to me before
Hotel System Main Page[^]

i can't use foreach statement in this case
sorry for this little question
but, how could i solve this?

Code:-
try
{
string filterExp = "FLOID = 1";
string sortExp = "";
DataRow[] drarray;
drarray = dB1_PureEyezDataSet.Room.Select(filterExp, sortExp, DataViewRowState.CurrentRows);
FLP1.Controls.Clear();
for (int i = 0; i < drarray.Length; i++)
{
Label B3 = new Label();
B3.Width = 104;
B3.Height = 80;
B3.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
B3.Dock = DockStyle.Fill;
B3.Anchor = AnchorStyles.None;
B3.TextAlign = System.Drawing.ContentAlignment.TopRight;
FLP1.Controls.Add(B3);
B3.BorderStyle = BorderStyle.FixedSingle;
}
}
catch { }
Henry Minute 1-Apr-11 10:31am    
You can still use foreach

foreach (DataRow dr in drArray)
{
Label B3 = new Label();
............................
............................
............................
B3.Background = (dr["RoomStatus"] == 1) ? Color.Red :.......... // you might have to use dr.Item["RoomStatus"] here, can't remember.
............................
............................
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900