tags:

views:

726

answers:

3

I have 3 drop down list having Date,Month ,Year.So when Updating I want this 3 Field to be a single Datafield in Sql Database. Iam using Asp.Net 2.0 Version(VB.Net).(Now these 3 Dropdown list values are saved as 3 Datafields in sql Database)

+2  A: 

I take it you're using a SqlCommand for this? Here's some sample, non-compiling code which I'll whip up for you here:

Dim cmd as SqlCommand("update tableX set dt = @var where ID = 1");
cmd.Parameters.AddWithValue("@var", new DateTime(CInt(day.SelectedValue), CInt(month.SelectedValue, CInt(year.SelectedValue)));

cmd.ExecuteNonQuery();
Dave Markle
Where is your validation?
Todd Smith
It blows and exception in the DateTime Constructor so you wont get bad dates in the DB but the user experience ain't going to be good.
Brody
If the DateTime constructor throws, you could always, uh, catch it.
Dave Markle
+5  A: 

Why not use a date/time picker instead? It seems to me to be a better idea than your own triplet of day, month and year.

Tim
This reduces the chance of honest mistakes. With a good calendar control, an honest user isn't going to enter "9/31/1977" or "2/29/1900". (I know that you can repopulate the day list in javascript to correspond to the correct # of days in the month.)
jrcs3
This also reduces a lot of code for checking valid dates and updating the other controls based on a user selection from another.
Tim
A: 

Join up the values:

month + "/" + day + "/" + year

and use DateTime.Parse() or DateTime.TryParse() to convert them to a date. If you are concerned about the date format for your culture, pass in a CultureInfo object. Add your own validation as needed.

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

Not foolproof, but it might satisfy your requirement.

H. Abraham Chavez