2

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

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
allrounder
  • 49
  • 6
  • i have also tried many other ways to do that but it gives the same error every time, String was not recognized as a valid DateTime. – allrounder Mar 14 '14 at 14:10
  • 1
    Well given *just* a minutes value, what `DateTime` are you trying to store? – Jon Skeet Mar 14 '14 at 14:11
  • i just want to store the minuts selected by user, these are 00, 15, 30, 45. I am not sure what should be the exact format for that. – allrounder Mar 14 '14 at 14:14
  • Can't you do `mora.Minutes`? That retrieves the minutes property from your `DateTime` object. This will only work *given* your parsing is successful. – theGreenCabbage Mar 14 '14 at 14:15
  • mora.minuts is not possible – allrounder Mar 14 '14 at 14:17
  • @user3420100: If you're just trying to store minutes, you shouldn't be using a `datetime` field in the database. – Jon Skeet Mar 14 '14 at 14:23
  • @Jon Skeet: yup i also concluded this. But for more practice i asked this question to know whether it is really not possible or i am doing any mistake. – allrounder Mar 14 '14 at 14:30
  • Well you can decide to set the minutes on some arbitrary date and time... but it's a really bad idea. – Jon Skeet Mar 14 '14 at 14:47

3 Answers3

3

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);
musefan
  • 47,875
  • 21
  • 135
  • 185
  • Precisely. Databases don't understand what `2014-03-11T02:00:00-07:00` means (you technically can store it as a string but the DB still doesn't know how to sort that given any situation you need to sort), which is why a common practice is to convert it to a unix timestamp and then store into a DB. Check out this thread: http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa – theGreenCabbage Mar 14 '14 at 14:13
  • @musefan: I have tried your solution it again gives the error "Input string was not in a correct format". – allrounder Mar 14 '14 at 14:27
  • @user3420100: Please provide an example of your input string – musefan Mar 14 '14 at 14:32
  • @musefan: I have the follwoing code at frond end and back end – allrounder Mar 14 '14 at 14:45
  • int minuts = int.Parse(drpPickuptime.SelectedItem.Text); DateTime dt = DateTime.Today.AddMinutes(minuts); cmd1.Parameters.AddWithValue("@pt", SqlDbType.DateTime).Value =dt; – allrounder Mar 14 '14 at 14:46
  • So the problem is your input is `":15"` so you need to remove that semi-colon (`:`) before you parse it to an int – musefan Mar 14 '14 at 15:18
0

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.

ryudice
  • 36,476
  • 32
  • 115
  • 163
0

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.

tlbignerd
  • 1,104
  • 9
  • 21