I have form where 4 buttons(Platinum, Diamond, Gold and Reset) are there, I have 3 Panel Control one for each of the first 3 buttons, what I want to do is that when I click on any one of the first three buttons the Panel for the respective buttons should appear on the screen and if any Panel is already on the screen it should not be visible. For e.g At any point of time when the application is running, consider that the Panel for the Platinum button is already on the screen and if I click on the Diamond button the Panel for the Diamond button should appear on the screen at the same place where Panel for the Platinum button was there, but the panel for the Platinum button which was already on the screen should disappear, I have a code written for this purpose, but however it is not performing as desired. Please help me find the error and also provide the solution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace rough2
{
    public partial class Form3 : Form
    {
        private List<Seat> list = new List<Seat>();
        //private Button[] buttons = new Button[225];
        //int i = 0;
        string clickedButton;
        private string connectionString = "Data Source=.\\SQLEXPRESS; Initial Catalog = RoughWork; Integrated Security = True;";
        char[] rows = "ABCDEFGHIJKLMNO".ToCharArray();
        int[] columns = Enumerable.Range(1, 15).ToArray();
        int[] xy = new int[] { 0, 0 };

        public Form3()
        {
            InitializeComponent();
        }

        private void btnPlatinum_Click(object sender, EventArgs e)
        {
            PlatinumPanel.Visible = true;
            DiamondPanel.Visible = false;
            GoldPanel.Visible = false;
            this.PlatinumPanel.Controls.Add(this.addPlatinumSeats());
            this.Controls.Add(this.PlatinumPanel);
        }

        private void btnDiamond_Click(object sender, EventArgs e)
        {
            PlatinumPanel.Visible = false;
            DiamondPanel.Visible = true;
            GoldPanel.Visible = false;
            this.DiamondPanel.Controls.Add(this.addDiamondSeats());
            this.Controls.Add(this.DiamondPanel);
        }

        private void btnGold_Click(object sender, EventArgs e)
        {
            PlatinumPanel.Visible = false;
            DiamondPanel.Visible = false;
            GoldPanel.Visible = true;
            this.GoldPanel.Controls.Add(this.addGoldSeats());
            this.Controls.Add(this.GoldPanel);
        }

        private Button addPlatinumSeats()
        {
            DataTable table = GetDataFromDataBase();
            SetOccupiedSeats(table);
            Button newbtn = new Button();
            newbtn.Visible = false;

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    // 1. find seat
                    var Number = String.Concat(rows[i], columns[j]);
                    var seat = list.Find(w => w.Number == Number);

                    // 2. create and position the button on form:
                    xy = PositioningSeat(j, i, xy[0], xy[1]);
                    seat.Name = String.Concat(rows[i], columns[j]);
                    seat.Text = String.Concat(rows[i], columns[j]);
                    seat.Size = new Size(36, 35);
                    seat.Location = new Point(xy[0], xy[1]);
                    seat.Click += new EventHandler(buttonSeats_Click);
                    seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
                    seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
                    seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
                    seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                    //seat.ForeColor = System.Drawing.Color.White;
                    //seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
                    //seat.FlatAppearance.BorderSize = 0;
                    //seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;                    
                    this.Controls.Add(seat);
                    //newbtn = seat;
                }
            }
            return newbtn;
        }

        private Button addDiamondSeats()
        {
            DataTable table = GetDataFromDataBase();
            SetOccupiedSeats(table);
            Button newbtn = new Button();
            newbtn.Visible = false;

            for (int i = 5; i < 10; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    // 1. find seat
                    var Number = String.Concat(rows[i], columns[j]);
                    var seat = list.Find(w => w.Number == Number);

                    // 2. create and position the button on form:
                    xy = PositioningSeat(j, i, xy[0], xy[1]);
                    seat.Name = String.Concat(rows[i], columns[j]);
                    seat.Text = String.Concat(rows[i], columns[j]);
                    seat.Size = new Size(36, 35);
                    seat.Location = new Point(xy[0], xy[1]);
                    seat.Click += new EventHandler(buttonSeats_Click);
                    seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
                    seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
                    seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
                    seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                    //seat.ForeColor = System.Drawing.Color.White;
                    //seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
                    //seat.FlatAppearance.BorderSize = 0;
                    //seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;                    
                    this.Controls.Add(seat);
                    //newbtn = seat;
                }
            }
            return newbtn;
        }

        private Button addGoldSeats()
        {
            DataTable table = GetDataFromDataBase();
            SetOccupiedSeats(table);
            Button newbtn = new Button();
            newbtn.Visible = false;

            for (int i = 10; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    // 1. find seat
                    var Number = String.Concat(rows[i], columns[j]);
                    var seat = list.Find(w => w.Number == Number);

                    // 2. create and position the button on form:
                    xy = PositioningSeat(j, i, xy[0], xy[1]);
                    seat.Name = String.Concat(rows[i], columns[j]);
                    seat.Text = String.Concat(rows[i], columns[j]);
                    seat.Size = new Size(36, 35);
                    seat.Location = new Point(xy[0], xy[1]);
                    seat.Click += new EventHandler(buttonSeats_Click);
                    seat.BackColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.PowderBlue;
                    seat.FlatAppearance.BorderColor = seat.Occupied ? System.Drawing.Color.Red : System.Drawing.Color.Blue;
                    seat.FlatAppearance.BorderSize = seat.Occupied ? 0 : seat.FlatAppearance.BorderSize = 1;
                    seat.FlatStyle = seat.Occupied ? System.Windows.Forms.FlatStyle.Popup : seat.FlatStyle = System.Windows.Forms.FlatStyle.Standard;
                    //seat.ForeColor = System.Drawing.Color.White;
                    //seat.FlatAppearance.BorderColor = System.Drawing.Color.Blue;
                    //seat.FlatAppearance.BorderSize = 0;
                    //seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;                    
                    this.Controls.Add(seat);
                    //newbtn = seat;
                }
            }
            return newbtn;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            using (SqlConnection cn = new SqlConnection(connectionString))
            {
                try
                {
                    cn.Open();
                    string ResetString = @"update SampleTable1 set IsBooked = 0 where SeatId<=225";
                    SqlCommand cmd = new SqlCommand(ResetString, cn);
                    cmd.ExecuteNonQuery();
                    Application.Restart();
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }

        private int[] PositioningSeat(int column, int row, int x, int y)
        {
            if (column == 0 || column == 15)
            {
                x = 60; // starting X position or reseting X to 1st column

                if (row == 0 || row == 5 || row == 10)
                {
                    y = 100;
                }

                //else if (row == 5)
                //{
                //    y = 290; //going to new sector of Y
                //}

                //else if (row == 10)
                //{
                //    y = 479; //going to new sector of Y
                //}

                else
                {
                    y = y + 37; // next seat for Y
                }
            }

            else if (column % 5 == 0)
            {
                x = x + 70; //going to new sector of X
            }

            else
            {
                x = x + 37; // next seat for X
            }

            return new int[] { x, y };

        }

        private void buttonSeats_Click(object sender, EventArgs e)
        {
            Seat seat = sender as Seat;
            clickedButton = seat.Number;

            if (seat == null)
                return;

            if (seat.Occupied)
            {
                MessageBox.Show("Seat Number " + seat.Text + " has already been taken!",
                                "Reservation Attention",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
            else
            {
                if (DialogResult.Yes ==
                    MessageBox.Show("Seat number " + seat.Text + " is free.\nDo you want to reserve it?",
                                    "New reservation",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question))
                {
                    //seat is being reserved...
                    seat.Occupied = true;
                    seat.BackColor = System.Drawing.Color.Red;
                    seat.FlatAppearance.BorderColor = System.Drawing.Color.Red;
                    seat.FlatAppearance.BorderSize = 0;
                    seat.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                    //MessageBox.Show("Reservation confirmed.\nSeat number " + seat.Text + " is now reserved.", "Reservation confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    using (SqlConnection cn = new SqlConnection(connectionString))
                    {
                        try
                        {
                            cn.Open();
                            string UpdateString = @"update SampleTable1 set IsBooked = 1 where SeatNo='" + clickedButton + "'";
                            SqlCommand cmd = new SqlCommand(UpdateString, cn);
                            cmd.ExecuteScalar();
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show("Error: " + ex.Message);
                        }
                    }
                }
            }
        }

        public class Seat : Button
        {
            private readonly DataRow _row;

            public Seat(DataRow row)
            {
                if (row == null)
                    throw new ArgumentNullException("row");
                _row = row;
            }

            public int ID
            {
                get
                {
                    return int.Parse(_row["SeatId"].ToString());
                }
            }

            public string Number
            {
                get
                {
                    return _row["SeatNo"] as string;
                }

                set
                {
                    if (value.Length <= 3)
                    {
                        _row["SeatNo"] = value;
                    }
                }
            }

            public bool Occupied
            {
                get
                {
                    return int.Parse(_row["IsBooked"].ToString()) == 1;
                }
                set
                {
                    _row["IsBooked"] = value ? 1 : 0;
                }
            }
        }

        private DataTable GetDataFromDataBase()
        {
            DataSet ds = new DataSet();
            DataTable table;
            using (SqlConnection cn = new SqlConnection(connectionString))
            {
                using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    da.SelectCommand = new SqlCommand { CommandText = @"select * from SampleTable1", Connection = cn };
                    da.Fill(ds, "SampleTable1");
                    table = ds.Tables["SampleTable1"];
                }
            }
            return table;
        }

        private void SetOccupiedSeats(DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                list.Add(new Seat(row));
            }
        }
    }
}

The Add..Seat methods are adding seats to the main form.
You need to add the seats to the appropriate panel!
E.g. at line 86 use this.PlatinumPanel.Contols.Add(seat); Additionally, each of the three Add...seats methods has a lot of common code.
Find where they differ, then using parameters make a common method.
E.g. AddSeats(Panel seatPanel) { ... } Assuming that you have added the panels using the form designer you should only need to change the visible property to show or hide them. (They should already be added to the controls collection!)

The Add..Seat methods are adding seats to the main form.
You need to add the seats to the appropriate panel!
E.g. at line 86 use this.PlatinumPanel.Contols.Add(seat); Additionally, each of the three Add...seats methods has a lot of common code.
Find where they differ, then using parameters make a common method.
E.g. AddSeats(Panel seatPanel) { ... } Assuming that you have added the panels using the form designer you should only need to change the visible property to show or hide them. (They should already be added to the controls collection!)

I made some changes as you said but now you see there is some strange problem , when I click on the Gold, Diamond, Platinum buttons to view their respective seats, without clicking on any of the seats(buttons) which appears below, you may also try, it that, when I click on those three buttons 2-3 times alternately and then click on any of the buttons to book the ticket, as soon as the ticket is booked, it gives message that "This seat is already booked" without even clicking on that same button for the second time. Please download the source code and database, so you may see what actually is happening? The attachment is here on the thread page.

The "already booked" message is appearing because you are reading the DB and adding the seats to list multiple times.

In particular this adds the click event handler to each button multiple times resulting in it being executed multiple times!

but I must do some paid for work for a while.

What's the meaning of above line? I could not get it.
So what's the solution for that?

Try changing you code to do the following:

On open app
Call

init panels

InitPanels()

Load DB in to DATASET (global)

Create seats and add to list (global)

Add seats to P,D,G panels

ClearPanels()
empty dataset, list and panels

On P/D/G button click

show required panel

(do nothing else)

On Seat click

set Occupied and update DB (as now)

On Reset click
ClearPanels
InitPanels

I am really not able to understand what is posted above, can you specify bit more clearly?

It means that I am in full time employment and helping you is voluntary.
I have given you some ideas via PM on how to improve your program.
You must now do the work.

I am really not able to understand what is posted above, can you specify bit more clearly?

It is psudo-code indicating a plan for how to re-structure your program to get it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.