ASP.NET - How to add dropdown list item from codebehind page

Asked By Sumit Kumar on 04-Apr-09 11:32 AM
Hi all,
What is the best way to write person's Date of Birth code from code behind page?I mean if i fill these value from Aspx page through the ListItem it will be too lengthy.And no idea what will be the range of YY item.I mean for MM 1 to 12,DD 1 to 31 and for YYYY???? So i think it will be better if i fill these value from code behind page through programitacally.

I have one more query.....in database i have set one field name DateOfBirth.If i pass the date of birth value from textbox these value directly will be saved in DateOfBirth field and easily can be fetched thru select query.And if i enter Date of birth from dropdownlist like MM DD YYYY for that i need to create three separate field in Person table like Month,Date,Year.Ok this way i can easily insert the value but how to retrieve these all three in one??I mean i want to display person details in that only field will be there  in label Date of Birth and in TextBox Date of birth will be displayed. Hope i explained my problem.

Any reply is highly appreciated.
Thanks,
Sumit

Use this. - [)ia6l0 iii replied to Sumit Kumar on 04-Apr-09 11:52 AM

1. It would be humanely impossible to determine the yyyy range that you need to provide for the person whose dob you need. Instead of having multiple listitems, combos, and writing client side validation code..and letting loose some "Y2K" sort of bugs, i would sincerly suggest you to have a  look at dateTime controls in built in ASP.Net toolkit or some thirdparty as the need be.

2. If you worried about formatting the date, then it should not be a worry. because you have the datetimeFormat and toString Overrides to take care of that. 
for e.g. if the calendar returns "4/4/2009 12:00AM" ,
you could then use, calendarDateValue.ToString("MM/dd/yyyy") to get it in the right format you need.

3. If you follow the step above, then these problems would be automatically gone.
Sunil Lakshkar replied to Sumit Kumar on 04-Apr-09 12:30 PM

hi

To add values in dropdown in code behind, you can add list item in dropdown at run time in for loop. for eg:

for(int i=1;i<=31;i++)

{   ListItem itm = new ListItem("i");

     MyDropDown.items.Add(itm);

}

2. For retreiving values from database, u can concatenate field values of MM, dd and YY and then return as:

  Select month + '-' + date +'-'+ year as DOB from MyTable

Regards,

Sunil


Re :: Date Problem - Shailendrasinh Parmar replied to Sumit Kumar on 04-Apr-09 03:34 PM

Hello Sumit,

Let the code insert the date as it is like mm/dd/yyyy hh:mm:ss format. But you can display the date in your aspx page with any kind of format even if it is the listitme of Dropdownlist.

Following is the link to the SQL Date Formats which you can use while firing the select query to fetch the date from database.

http://www.sql-server-helper.com/tips/date-formats.aspx

So you need not to worry and also no need to make three separate fields in the table.

And while inserting in the database, if you are using calender control, its ok otherwise convert the text of Birthdate text to DateTime using Convert.ToDateTime statement.

Hope this helps.

sanjay replied to Sunil Lakshkar on 09-Jul-10 06:03 AM

Sunil,


You mentioned we can concatenate and then use the select statement as you provided.


How would the concatenate work? Would you be able to provide some sample code?


Also how would it know which record's date of birth to display in my drop down?


There may be many records in my table?


Thanks.