I have monthcalendar with a button, when a button is clicked and a selected date in monthcalendar is equal to a value from database column then Iam usin a listbox.datasource from the database, but after that when I select a new date in monthcalendar and selected date is not equal to the column value i want back the first datasource for the list I used. Note that I use just: if(date.Equals("20160322")) without the foreach it works fine but not with the the foreach to loop throught the column
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < defaultList.Length; i++)
{
listBox1.Items.Add(defaultList[i]);
}
string date = monthCalendar1.SelectionStart.Date.ToString("yyyyMMdd");
string connetionString = null;
MySqlConnection connection;
MySqlCommand command;
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataSet ds = new DataSet();
int i = 0;
string sql = null;
connetionString = "datasource=localhost; database=bokning;port=3306;username=root;password=666666";
sql = "select date,dayTime from newsystem where date='" + date + "'";
connection = new MySqlConnection(connetionString);
try
{
connection.Open();
command = new MySqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
dtDatetime = ds.Tables[0];
foreach (DataRow dr in dtDatetime.Rows)
{
if (date.Equals(dr["date"]))
{
listBox1.DataSource = ds.Tables[0];
listBox1.ValueMember = "date";
listBox1.DisplayMember = "dayTime";
}
else
{
//listBox1.Items.Clear();
listBox1.DataSource = defaultList;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Cannot open connection ! ");
}
}