Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataTable problem!

DataTable problem!

Scheduled Pinned Locked Moved C#
databasexmlhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    visiontec
    wrote on last edited by
    #1

    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 here

    if(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

    T 1 Reply Last reply
    0
    • V 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 here

      if(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

      T Offline
      T Offline
      turbochimp
      wrote on last edited by
      #2

      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...’

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups