Reputation: 3130
I have a textbox without calendar control and we will call it as publication year(we use diff name in our project). The publication year is 1999, 2001 or 1860.
My step goes like this
I have datetime in my database.(unchangable)
I am using C# and sql server 2005.
string date = textBox1.text;
Datetime dt = Convert.ToDateTime(date);
I pass dt to my database using dynamic sql
to trick the code, I added a prefix 1/1/ before my entered date like 1/1/2010 , it worked well
Now my TL asked me not to do like that.... I am ??? . Please help
Upvotes: 2
Views: 804
Reputation: 15253
You could use a drop down list with the available years? If there are only three possible publication years, then this would make more sense as an input control. Grab the year and pad the DateTime month and day values with "1" each.
Upvotes: 0
Reputation: 88074
As a note, if you only care about the year then I would highly suggest that the database field should be an Int instead of a DateTime.
The minimum sql datetime value is 1/1/1753 for a number of reasons that you can certainly look up. However, the point is that a regular DateTime field is storing way more information than you care about.
So if you deal with publications older than 1753 you're going to run into an architectural issue. And, if somehow you didn't, you'd certainly run into issues deciding if a publication was 280 years old or 279 or 281...
Upvotes: 4
Reputation: 1500893
It sounds like you should really:
DateTime
with that year, and January 1st as the day/month.For example:
int year;
if (int.TryParse(textBox1.Text, out year))
{
DateTime dt = new DateTime(year, 1, 1);
}
else
{
// Handle input
}
Upvotes: 8