DataTable problem!
-
Hi there I have a problem in using a data table object created from a dataset object. I have a form that has the following controls: 2 textboxs : txtTodayEvents , txtTodayDescr 1 listbox : lbToday 1 label : lblToday The database i am using has the following fields: ID EventName Description EventDate This is the code that i am using to get my data table working:
private void TodaysEvents()
{
DataSet dsAllEvents = new DataSet("Events");
dsAllEvents.ReadXml("EventsDB.xml");
// i use an acess DB but this is to show
// that i got the dataset filled.string date = DateTime.Now.ToShortDateString();
int todEve = dsAllEvents.Tables["Events"].Select("EventDate Like '*" + date + "*'").Length; //works gr8 uptill hereif(todEve>=1)
{
DataTable TodTable = new DataTable("TE");
TodTable.Columns.Add("ID",System.Type.GetType("System.String"));
TodTable.Columns.Add("EventName",System.Type.GetType("System.String"));
TodTable.Columns.Add("Description");
TodTable.Columns.Add("EventDate",System.Type.GetType("System.String"));TodTable.Rows.Add(dsAllEvents.Tables\["Events"\].Select("EventDate Like '\*" + date + "\*'")); lbToday.DataSource = TodTable; lbToday.DisplayMember = "EventDate"; lbToday.Visible = true; txtTodayEvent.DataBindings.Add("Text",TodTable,"EventName"); txtTodayEvent.Visible = true; txtTodayDescr.DataBindings.Add("Text",TodTable,"Description"); txtTodayDescr.Visible = true;
}
else
{
lblToday.Visible = true;
}
}when i run this code it runs but i get nothing in my controls Can anyone plz tell me what i am doing wrong? VisionTec
-
Hi there I have a problem in using a data table object created from a dataset object. I have a form that has the following controls: 2 textboxs : txtTodayEvents , txtTodayDescr 1 listbox : lbToday 1 label : lblToday The database i am using has the following fields: ID EventName Description EventDate This is the code that i am using to get my data table working:
private void TodaysEvents()
{
DataSet dsAllEvents = new DataSet("Events");
dsAllEvents.ReadXml("EventsDB.xml");
// i use an acess DB but this is to show
// that i got the dataset filled.string date = DateTime.Now.ToShortDateString();
int todEve = dsAllEvents.Tables["Events"].Select("EventDate Like '*" + date + "*'").Length; //works gr8 uptill hereif(todEve>=1)
{
DataTable TodTable = new DataTable("TE");
TodTable.Columns.Add("ID",System.Type.GetType("System.String"));
TodTable.Columns.Add("EventName",System.Type.GetType("System.String"));
TodTable.Columns.Add("Description");
TodTable.Columns.Add("EventDate",System.Type.GetType("System.String"));TodTable.Rows.Add(dsAllEvents.Tables\["Events"\].Select("EventDate Like '\*" + date + "\*'")); lbToday.DataSource = TodTable; lbToday.DisplayMember = "EventDate"; lbToday.Visible = true; txtTodayEvent.DataBindings.Add("Text",TodTable,"EventName"); txtTodayEvent.Visible = true; txtTodayDescr.DataBindings.Add("Text",TodTable,"Description"); txtTodayDescr.Visible = true;
}
else
{
lblToday.Visible = true;
}
}when i run this code it runs but i get nothing in my controls Can anyone plz tell me what i am doing wrong? VisionTec
I can't tell what data type you're using for EventDate in the "Events" DataTable, but if it's a DateTime it may be worthwhile to use a BETWEEN clause or >= and <= to compare dates (start at midnight, and end at 11:59:59, for instance) instead of LIKE. The bigger issue is that a call to Datatable.Select returns an array of DataRow objects (DataRow[]) from 0 to n members in length, not an integer. That said, the lines that read:
int todEve = dsAllEvents.Tables["Events"].Select("EventDate Like '*" + date + "*'").Length;
if(todEve>=1)should read something like:
DataRow[] foundRows = dsAllEvents.Tables["Events"].Select("EventDate Like '*" + date + "*'").Length;
if(foundRows.Length >= 1)One more small note; instead of
System.Type.GetType("System.String")
you can shorthand with
typeof(string)
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’