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. Problem with Date Validation

Problem with Date Validation

Scheduled Pinned Locked Moved C#
helptutorial
4 Posts 3 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
    Verghese
    wrote on last edited by
    #1

    Below pasted is the piece of code where I want to display records based on the date range. Let's say I have a startDate(01/15/2007) and an endDate(01/15/2008): Can anyone give me some inputs as how to handle date validation in this code. I mean the place where I'm concatenating those 4 columns and getting a single date column row by row, I want to have this date validation. If it falls within this range, it shud add to the DataSet as a new row or else no.

    DataSet newPDS = new DataSet();
    DataRow row = new DataRow();

    //ADM_DATE is DateTime field in DataSet

    //Checking whether column is already created or not
    if (newPDS.Tables[0].Columns["ADM_DATE"]== null)

    {
    DataColumn dCol = new DataColumn(newPDS.Tables[0].Columns.Add("ADM_DATE", typeof(DateTime), "").ToString());
    }

    for(Int32 i = 0; i < newPDS.Tables[0].Rows.Count; i++)
    {
    row = newPDS.Tables[0].Rows[i];
    //Concatenating 4 columns and filling up a date column (ADM_DATE) row by row
    row["ADM_DATE"] = row["CADMMM"] + "/" + row["CADMDD"] + "/" + row["CADMHH"] + row["CADMYY"];
    }

    Any help would be appreciated. Thanking you in anticipation.

    modified on Wednesday, August 6, 2008 3:34 PM

    G 1 Reply Last reply
    0
    • V Verghese

      Below pasted is the piece of code where I want to display records based on the date range. Let's say I have a startDate(01/15/2007) and an endDate(01/15/2008): Can anyone give me some inputs as how to handle date validation in this code. I mean the place where I'm concatenating those 4 columns and getting a single date column row by row, I want to have this date validation. If it falls within this range, it shud add to the DataSet as a new row or else no.

      DataSet newPDS = new DataSet();
      DataRow row = new DataRow();

      //ADM_DATE is DateTime field in DataSet

      //Checking whether column is already created or not
      if (newPDS.Tables[0].Columns["ADM_DATE"]== null)

      {
      DataColumn dCol = new DataColumn(newPDS.Tables[0].Columns.Add("ADM_DATE", typeof(DateTime), "").ToString());
      }

      for(Int32 i = 0; i < newPDS.Tables[0].Rows.Count; i++)
      {
      row = newPDS.Tables[0].Rows[i];
      //Concatenating 4 columns and filling up a date column (ADM_DATE) row by row
      row["ADM_DATE"] = row["CADMMM"] + "/" + row["CADMDD"] + "/" + row["CADMHH"] + row["CADMYY"];
      }

      Any help would be appreciated. Thanking you in anticipation.

      modified on Wednesday, August 6, 2008 3:34 PM

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Why do you have the date stored in four columns in the first place? That makes it mostly useless... You are putting a string in the column, and as the format is not based on ISO 8601 it's not comparable. Create a DateTime value instead: row["ADM_DATE"] = new DateTime((int)row["CADMHH"] * 100 + (int)row["CADMYY"], (int)row["CADMMM"], (int)row["CADMDD"]); ...or, if the values are not even numerical (which makes them even more useless...): row["ADM_DATE"] = new DateTime(int.Parse((string)row["CADMHH"]) * 100 + int.Parse((string)row["CADMYY"]), int.Parse((string)row["CADMMM"]), int.Parse((string)row["CADMDD"]));

      Despite everything, the person most likely to be fooling you next is yourself.

      V 1 Reply Last reply
      0
      • G Guffa

        Why do you have the date stored in four columns in the first place? That makes it mostly useless... You are putting a string in the column, and as the format is not based on ISO 8601 it's not comparable. Create a DateTime value instead: row["ADM_DATE"] = new DateTime((int)row["CADMHH"] * 100 + (int)row["CADMYY"], (int)row["CADMMM"], (int)row["CADMDD"]); ...or, if the values are not even numerical (which makes them even more useless...): row["ADM_DATE"] = new DateTime(int.Parse((string)row["CADMHH"]) * 100 + int.Parse((string)row["CADMYY"]), int.Parse((string)row["CADMMM"]), int.Parse((string)row["CADMDD"]));

        Despite everything, the person most likely to be fooling you next is yourself.

        V Offline
        V Offline
        Verghese
        wrote on last edited by
        #3

        Guffa wrote:

        Why do you have the date stored in four columns in the first place? That makes it mostly useless...

        that's infact a million $$$ question...........the dates stored in AS400 (mainframes) systems are in this format only...........and moreover, they dont have a date field as such.......... (i said wtf???? wen i first saw this format, thn i learned its Mainframes).......... :) anyway, with couple of changes in the code i've done the date validation.........but its showing all records in the report............i don't want it that way............date validation is working fine, but its working only on the "ADM_DATE" column......... hold on, i'll explain.........suppose a row of record falls within the given date range, then in the report as expected its displaying the entire row............but when its not falling within the date range, still its displaying the row of record, but keeping the date field as empty............ did i explained the situation clearly..........pls see the code below:

        for(Int32 i = 0; i < newPDS.Tables[0].Rows.Count; i++)
        {
        row = newPDS.Tables[0].Rows[i];
        string stringConCatDate;
        DateTime startDate = DateTime.Parse("07/28/1960");
        DateTime endDate = DateTime.Parse("02/14/1982");

        stringConCatDate = row\["CADMMM"\] + "/" + row\["CADMDD"\] + "/" + row\["CADMHH"\] + row\["CADMYY"\];
        
        DateTime dtConCatDate = DateTime.Parse(stringConCatDate);
        
        if (dtConCatDate > startDate && dtConCatDate < endDate)
        {
        	row\["ADM\_DATE"\] = stringConCatDate;
        }
        

        }

        myReport.SetDataSource(newPDS.Tables["TABLE_1"]);

        K 1 Reply Last reply
        0
        • V Verghese

          Guffa wrote:

          Why do you have the date stored in four columns in the first place? That makes it mostly useless...

          that's infact a million $$$ question...........the dates stored in AS400 (mainframes) systems are in this format only...........and moreover, they dont have a date field as such.......... (i said wtf???? wen i first saw this format, thn i learned its Mainframes).......... :) anyway, with couple of changes in the code i've done the date validation.........but its showing all records in the report............i don't want it that way............date validation is working fine, but its working only on the "ADM_DATE" column......... hold on, i'll explain.........suppose a row of record falls within the given date range, then in the report as expected its displaying the entire row............but when its not falling within the date range, still its displaying the row of record, but keeping the date field as empty............ did i explained the situation clearly..........pls see the code below:

          for(Int32 i = 0; i < newPDS.Tables[0].Rows.Count; i++)
          {
          row = newPDS.Tables[0].Rows[i];
          string stringConCatDate;
          DateTime startDate = DateTime.Parse("07/28/1960");
          DateTime endDate = DateTime.Parse("02/14/1982");

          stringConCatDate = row\["CADMMM"\] + "/" + row\["CADMDD"\] + "/" + row\["CADMHH"\] + row\["CADMYY"\];
          
          DateTime dtConCatDate = DateTime.Parse(stringConCatDate);
          
          if (dtConCatDate > startDate && dtConCatDate < endDate)
          {
          	row\["ADM\_DATE"\] = stringConCatDate;
          }
          

          }

          myReport.SetDataSource(newPDS.Tables["TABLE_1"]);

          K Offline
          K Offline
          Kjetil Svendsen
          wrote on last edited by
          #4

          Verghese wrote:

          did i explained the situation clearly..........pls see the code below:

          Not sure :) Seems like you want to display only the rows witch have a non-empty date field. Try

          myReport.SetDataSource(newPDS.Tables["TABLE_1"].Select("ADM_DATE IS NOT NULL"));

          I'm not sure if this works with the SetDataSource method, but try it. Kjetil

          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