I have a Windows Form where 225 buttons are added at the run time, also I have the database which have two columns (first column is the SeatNumber with the datatype nchar(4) and the other column is IsBooked with the datatype bit). The SeatNumber contains data like this, A1,A2,A3,A4................O13,O14,O15, The other column contains value either 0 or 1. What I want to do is that when the form loads it should check in the database which rows of the second column has the value 1, for e.g. If A1,A2,A3,A4 and B1,B2,B3,B4 has the value 1 the buttons with the same name i.e buttons A1,A2,A3,A4,B1,B2,B3 and B4 should appear RED when the form loads. I have written the code, but it is not working. Can anyone please help me.
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 rough1
{
public partial class Form3 : Form
{
List<Seat> list = new List<Seat>();
Button[] buttons = new Button[225];
//int i = 0;
string connectionString = "Data Source=DESAI-PC\\SQLEXPRESS; Initial Catalog = RoughWork; Integrated Security = True;";
public Form3()
{
InitializeComponent();
///////
//Buttons (Seats) Creation
//////
//Button[] buttons = new Button[225];
//list = new List<Seat>();
//Constants 1:
char[] rows = "ABCDEFGHIJKLMNO".ToCharArray();
int[] columns = Enumerable.Range(1, 15).ToArray();
int[] xy = new int[] { 0, 0 };
for (int i = 0; i < rows.Length; i++)
{
for (int j = 0; j < columns.Length; j++)
{
// 1. add seat to a generic list<T>:
Seat s = new Seat();
s.Number = String.Concat(rows[i], columns[j]);
//when creating seats set occupation on false (seat free)!
s.Occupied = false;
list.Add(s);
// 2. create and position the button on form:
xy = PositioningSeat(j, i, xy[0], xy[1]);
buttons[i] = new Button();
buttons[i].Name = String.Concat(rows[i], columns[j]);
buttons[i].Text = String.Concat(rows[i], columns[j]);
buttons[i].Size = new Size(35, 35);
buttons[i].Location = new Point(xy[0], xy[1]);
buttons[i].Click += new EventHandler(buttonSeats_Click);
buttons[i].BackColor = System.Drawing.Color.PowderBlue;
//buttons[i].ForeColor = System.Drawing.Color.White;
//buttons[i].FlatAppearance.BorderColor = System.Drawing.Color.Blue;
//buttons[i].FlatAppearance.BorderSize = 0;
//buttons[i].FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.Controls.Add(buttons[i]);
}
}
DataTable table = GetDataFromDataBase();
SetOccupiedSeats(table);
}
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 % 5 == 0)
{
y = y + 50; //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)
{
Button seat = sender as Button;
Button selected = sender as Button;
//MessageBox.Show(selected.Name);
if (list.Where(w => w.Number == seat.Text).Select(s => s.Occupied).Contains(true))
{
MessageBox.Show("Seat Number " + seat.Text + " has already been taken!", "Reservation Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (DialogResult.Yes == MessageBox.Show("Set number " + seat.Text + " is free.\nDo you want to reservate it?", "New reservation", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
//seat is being reserved...
list.Where(w => w.Number == seat.Text).Select(s => { s.Occupied = true; return s; }).ToList();
seat.BackColor = System.Drawing.Color.Red;
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);
}
}
}
public class Seat
{
public string Number { get; set; }
public bool Occupied { get; set; }
}
private DataTable GetDataFromDataBase()
{
DataTable table = new DataTable();
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
da.SelectCommand.CommandText = @"select * from SampleTable1";
da.SelectCommand.Connection = cn;
da.Fill(table);
}
}
return table;
}
private void SetOccupiedSeats(DataTable table)
{
foreach (DataRow row in table.Rows)
{
int seatNumber = int.Parse(row["SeatId"].ToString());
int seatOccupied = int.Parse(row["IsBooked"].ToString());
string seatNumbr = Convert.ToString(seatNumber);
if (seatOccupied == 1)
{
list.Where(w => w.Number == seatNumbr).Select(s => { s.Occupied = true; return s; }).ToList();
}
}
}
}
}