Reputation: 49
hello i'm new to programming i have the following problem
string tt = drpPickuptime.SelectedItem.Text;
DateTime mora = Convert.ToDateTime(tt);
I have also tried this
string tt = drpPickuptime.SelectedItem.Text;
DateTime mora = DateTime.Parse(tt, new CultureInfo("fr-FR")).Date;
and this 1 also
DateTime mora = DateTime.ParseExact(drpPickuptime.SelectedItem.Text, "mm", CultureInfo.CurrentCulture);
but the error is nor rectified. The problem is that i have a combobox and i have put just minuts there which will be selected by user. I want to store that value in database. But in database the data type of that field is datetime. When i change that to string the problem is solved. But isn't it possible to store that string in database with required conditions. Though it is not a good question but i have done my all effort on it and could;nt get the result. Can anyone help me please
Upvotes: 2
Views: 180
Reputation: 1114
You can't store minutes as a DateTime in the database, as @Jon mentioned. You'd have to store the value as an integer. Then if you want to apply those minutes to some date value, you could do:
DateTime d = DateTime.Now.AddMinutes(tt);
or if you want those minutes in the past you could do:
DateTime d = DateTime.Now.AddMinutes(tt * -1);
You could also store it in the database as a string. Then in your code you can do an int.Parse to convert it from a string to an integer.
Upvotes: 0
Reputation: 37366
I think the best would be to change the datatype to bigint, and store it as a timespan. You can store the Ticks property of the timespan in the database, then when you bring it back you just do TimeSpan.FromTicks(database field)
. And to store it you can do TimeSpan.FromMinutes(your comobobox value)
, you will have to parse the combobox value to an integer.
Upvotes: 0
Reputation: 48415
The problem is that you cannot create a valid DateTime with just minutes, there needs to be a date (year, month, day).
If you are just storing minutes in the database then I would recommend an int
type. If you really must have a DateTime then you will need to store a date too, perhaps Today's date is an option? in which case you can do this:
int minutes = int.Parse(drpPickuptime.SelectedItem.Text);
DateTime dt = DateTime.Today.AddMinutes(minutes);
Upvotes: 3