hello i'm trying to get some data from a database and appending them to a listview in C#, the data i'm trying to retrieve is between an user specified startdate and enddate. i'll try to be as specific as i can:
i'm using an access .mdb database
i'm developing in visual C# 2010, winforms.
so i've got a form made by one combobox and a listView, the ComboBox has 12 items, one for each month of the year and what i'm trying to achieve is to append data from the access database to the listview that is in that month.
The relevant code is as follows:
this is the event handler for item selected change for the ComboBox, this is where i do the important stuff:
private void cbMonth_SelectedIndexChanged(object sender, EventArgs e)
{
//CONEXION is a constant string with the path to the database
connectDB = new OleDbConnection(CONEXION);
//getting the selected month from the combo
int month = cbMonth.SelectedIndex;
//instanciating a string for the query
string query = getQuery(month);
// debugging XD
MessageBox.Show(query);
// this will be the dataTable with the data retrieved
DataTable EventTable = connectDB(query);
//filling the listview
fillListView(EventTable);
conexionDB.Close();
}
Now this method is the one that returns the query string, it is fairly straight forward but i think the problem is in here so please pay attention xd
public string getQuery(int month)
{
string query = "";
//just placeholders
DateTime ini = DateTime.Now, fin = DateTime.Now;
switch (month)
{
case 0:
ini = DateTime.Parse("01/01/2013");
fin = DateTime.Parse("31/01/2013");
break;
case 1:
ini = DateTime.Parse("01/02/2013");
fin = DateTime.Parse("28/02/2013");
break;
case 2:
ini = DateTime.Parse("01/03/2013");
fin = DateTime.Parse("31/03/2013");
break;
case 3:
ini = DateTime.Parse("01/04/2013");
fin = DateTime.Parse("30/04/2013");
break;
case 4:
ini = DateTime.Parse("01/05/2013");
fin = DateTime.Parse("31/05/2013");
break;
case 5:
ini = DateTime.Parse("01/06/2013");
fin = DateTime.Parse("30/06/2013");
break;
case 6:
ini = DateTime.Parse("01/07/2013");
fin = DateTime.Parse("31/07/2013");
break;
case 7:
ini = DateTime.Parse("01/08/2013");
fin = DateTime.Parse("31/08/2013");
break;
case 8:
ini = DateTime.Parse("01/09/2013");
fin = DateTime.Parse("30/09/2013");
break;
case 9:
ini = DateTime.Parse("01/10/2013");
fin = DateTime.Parse("31/10/2013");
break;
case 10:
ini = DateTime.Parse("01/11/2013");
fin = DateTime.Parse("30/11/2013");
break;
case 11:
ini = DateTime.Parse("01/12/2013");
fin = DateTime.Parse("31/12/2013");
break;
}
query = "select * from Events where (EventsDate between #" +ini.ToShortDateString() + "# and #" + fin.ToShortDateString() + "#)";
return query;
}
This method returns the expected query as i've tested it
now comes the method to make the query, i think this method is fairly simple as well
public DataTable connectDB(string query)
{
conexionDB = new OleDbConnection(CONEXION);
//making the query
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conexionDB);
// filling a datatable
DataTable EventTable= new DataTable();
adapter.Fill(EventTable);
return EventTable;
}
now this is the method to fill the listview
public void fillListView(DataTable EventsTable)
{
for (int i = 0; i < EventsTable.Rows.Count; i++)
{
DataRow row = EventosTable.Rows[i];
string nae= row["EventName"].ToString();
MessageBox.Show(name);
DateTime date = DateTime.Parse(fila["EventPrice"].ToString());
int price = int.Parse(fila["EvenPrice"].ToString());
string description = fila["EventDescrip"].ToString();
Event myevent = new Event();
myevent.Name = name;
myevent.Date= date;
myevent.Price= price;
myevent.Description= description;
EventsListView.Add(myevent);
ListViewItem item = new ListViewItem();
item.Text = name;
item.SubItems.Add(date.ToString());
item.SubItems.Add(price.ToString());
item.SubItems.Add(description.ToString());
EventsListView.Items.Add(item);
}
}
well everything works almost fine.. the problem is that if i select a month greater to any event in the combobox, the listview will load every month that is <= to the month selected, say if i have an event with eventDate "23/02/2013" and another event with eventDate "20/12/2013" and i select december in the combobox the listview instead of loading just the event that has the corresponding date to december, it loads both of the events.
so it's like the query was saying , load every event with eventDate <= to month selected.
help?
La solution
try one of these 2 things:
1) change your date strings to be in the MM/dd/yyyy format:
case 10:
ini = DateTime.Parse("12/01/2013");
fin = DateTime.Parse("12/31/2013");
break;
2) or specify the format using the DateTime.ParseExact() method:
case 11:
ini = DateTime.ParseExact("01/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
fin = DateTime.ParseExact("31/12/2013", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
break;