Link to home
Start Free TrialLog in
Avatar of spanish_fly
spanish_fly

asked on

Problem with Date Validation

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) in the format of "MM-DD-YYYY" :

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.

Any help would be appreciated. Thanking you in anticipation.


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"];
}

Open in new window

Avatar of burakiewicz
burakiewicz
Flag of United States of America image

DateTime start=DateTime.Parse("01/15/2007");
            DateTime end = DateTime.Parse("01/15/2008");
            DateTime dateTocheck;
            string date = row["CADMMM"] + "/" + row["CADMDD"] + "/" + row["CADMHH"] + row["CADMYY"];
            if (DateTime.TryParse(date,out dateTocheck))
            {
                if (dateTocheck>=start && dateTocheck<=end)
                {
                    row["ADM_DATE"] = dateTocheck; // || date
                }
            }
Avatar of spanish_fly
spanish_fly

ASKER

Thanks burakiewicz...............with little changes the date validation seems working, but one problem arises here..................as expected the validation is checking the date added in each row and it validates it perfectly only for the "ADM_DATE" column, by this I mean that even if a record doesn't falls within that date range, still it is being displayed but without the date field (i.e the date field for such records will be blank) and for the one which it satisfies the date condition it displays all fields alongwith the date field.

The bottom line is that : "I don't want my report to display those records whose date fields comes blank in the report".

To be more clear, pls see the attached file. It say's the whole story.

The changed code is also attached 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;
	}

Open in new window

2.pdf
you could remove those rows from the dataSet, or you could have another datatable that you add the rows to that passes the validation

above your for loop add this
DataTable dt = newPDS.Tables[0].Clone;

and at the bottom of the loop put
if(!string.IsNullOrEmpty(stringConCatDate)
{
dt.Rows.Add(row);
}
then use dt as the datasource
I think I was unable to explain it clearly..............my date column in database is not null or empty.............just on the basis of our date validation, its not showing up the date field for some records in the report.............if u have seen the attached PDF file, you wud have noticed that for certain records the date field is not displayed, it doesnt means that the date field is blank, its bcoz of the fact that after our date validation, that particular date doesn't falls within that range...............

so, i'm looking forward to remove those records (not displaying the whole row), which doen't falls within the given date range.............

did i explained it clearly this time...............thanks in anticipation
ASKER CERTIFIED SOLUTION
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this
  newPDS.Tables[0].Rows[0].Delete();

should be
  newPDS.Tables[0].Rows[i].Delete();
Thanks.................but its showing the exception as : "There is no row at position 10" and the report is not getting loaded.



use
Int32 i = newPDS.Tables[0].Rows.Count-1; i >=0 ; i--)
i already did tried that "count-1"............but all in vain................!!!

Please see the details when I use the following combination:

*******************************************************************************************
for(Int32 i = newPDS.Tables[0].Rows.Count; i > 0 ; i--)
{
              row = newPDS.Tables[0].Rows[i];

Note : Throws an exception just after the above line "There is no row at position 10"
*******************************************************************************************
for(Int32 i = newPDS.Tables[0].Rows.Count-1; i >=0 ; i--)
Note :  It executes the loop properly but neither loads the report nor throws any exception.
*******************************************************************************************
for(Int32 i = newPDS.Tables[0].Rows.Count-1; i >=0 ; i--)
Note : If i remove the ELSE condition - (newPDS.Tables[0].Rows[i].Delete(); )
then I get the same report as i mentioned on top, i.e. entire set of records but some of them without date fields (and those are the one's which doesn't falls within the date range)

*******************************************************************************************

for example 2 does it alway end up hitting the row delete???
at the end of the loop does it show any rows in the
newPDS.Tables[0].Rows.Count
In Example-2, the "IF" condition is also working properly.................for the date which falls within the range, it adds it into the dataset using (row["ADM_DATE"] = stringConCatDate;)..................and for the one which doesn't falls within the range, it moves onto the "ELSE" part using (newPDS.Tables[0].Rows[i].Delete();).................!!!
At the end of the loop, when the value of "i" goes -1 (minus one)...............it simply exits the loop and there's now row at : "newPDS.Tables[0].Rows.Count"
 
one more point to add : when it moves onto the delete part, i'm unable to check what its actually doing.....................and moreover as per Example-3, when i remove the ELSE part, it displays the report also...............so i guess there's some problem with the DELETE thing..............but I'm not sure abt it................
hmmm  it should just be deleting the row at that position.Step through the loop and after the if else, check the count to see if it ever goes down, or for some reason always goes down
amidst these, i'm struck @ something different.................in my database, as i mentioned the date fields are in MM, DD, HH, YY format (i.e. Today'd date : 08, 07, 20, 08).
In the database records when "HHYY" is an year like 2007. It will be in database as 20 and 07. And in database the "07" automatically becomes "7" and upon concatenation the year is just shown as "207" instead of "2007".
please help.
this should do it
like string year=row["CADMYY"];
if (year.Length<2)
{
  year="0" + year;
}

or in 1 line
row["CADMYY"]=row["CADMYY"].ToString().Length<2 ? "0"+row["CADMYY"] : row["CADMYY"];
Thanks for that..................the year length thing is fine now................!!!!
now i'm back to the actual issue...............while debugging i noticed that the row count doesn't exceeds or lower than the actual count.............it moves as per the correct row count..................but dont know why its not loading the report......................
meanwhile, if something interesting goes thru ur mind, please lemme know............
 
the only other thing i can think to try is


then use the dt as the datasource
DataTable dt = newPDS.Tables[0].Clone;//Copies the structure
for(Int32 i = newPDS.Tables[0].Rows.Count; i >=0 ; 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;
    dt.Rows.Add(row);
}
else
{
 
}
}

Open in new window

still showing the same exception : "There is no row at position 10"......................AMEN
sorry, i copied the old code you need the -1
DataTable dt = newPDS.Tables[0].Clone;//Copies the structure
for(Int32 i = newPDS.Tables[0].Rows.Count-1; i >=0 ; 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;
    dt.Rows.Add(row);
}
else
{
 
}
}
even i didnt noticed tht and just simply copied................i tried this too............ now when the "IF" loop executes for the first time and enters the TRUE condition, it throws an exception after this line :
myTable.Rows.Add(row);
Exception : "This row already belongs to another table".
 
 
hi............it seems as if i'm on the verge on getting to solution..............!!!
I'm still strucked on the same error, as soon as I add a row to myTable it throws an exception:

myTable.Rows.Add(row);
Exception : "This row already belongs to another table".
Please suggest. Thanks in anticipation
Perfect Job............!!!

hi............it seems as if i'm on the verge on getting to solution..............!!!
I'm still strucked on the same error, as soon as I add a row to myTable it throws an exception:

myTable.Rows.Add(row);
Exception : "This row already belongs to another table".
Please suggest. Thanks in anticipation
GOTCHA.................here's the solution..............!!!
myTable = newPDS.Tables[0].Clone();
Instead of doing :
dt.Rows.Add(row);
use :
dt.ImportRow(row);
Your original code is trying to take a row that is in another table and add it to a different table, which is not allowed, ImportRow makes a copy of the passed in row and adds the copy to the table instead of trying to add the original row.
More info on the same topic at the following link:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21423689.html

 
cool, glad you got it
burakiewicz, I really appreciate you for all the help extended.
Thanks a lot once again.