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): 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
-
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
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.
-
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.
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"]);
-
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"]);
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