Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have asp.net c#
computing total number of hours.
since i will pull the data from a database as a string value, i must convert that string to a datetime which seems not working at all.

the value 2015, 4, 12, 22, 28, 20 must be a string ( i will pull the data from a database in a later plan). and put that string inside "new DateTime(stringvalue)"

take a look at my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication13
{
public partial class _Default : System.Web.UI.Page
{
DateTime now;
//string date = "2015, 4, 12, 22, 28, 20";
protected void Page_Load(object sender, EventArgs e)
{
now = new DateTime(2015, 4, 12, 22, 28, 20); // year, Month, day, hours, minutes, seconds
//now = Convert.ToDateTime(date);
}

protected void Button1_Click(object sender, EventArgs e)
{
Label3.Text = DateTime.Now.ToString();
Label2.Text = now.ToString();
TimeSpan diff = DateTime.Now - now;
double hours = diff.TotalHours;
Label1.Text = "total number of hours is: " + hours.ToString();
}
}
}
Posted
Comments
Abdul Samad KP 13-Apr-15 11:16am    
Why you are saving date as string in the DB?
Any way you can use DateTime.ParseExact to convert string to date
Have a look at https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx

Don't.

Never store DateTime values in a DB as a string: parse them at point of input (or as close as you can get) because only then can you tell what format the user has input them in, and that they are correct. It's far too late by the time they get to the database as they have lost all user context, and if they are wrong: "74th Smegruary 4215" it's too late to reject the data and get it entered correctly. And since you lost all context, you don't know what date "01/02/03" is either! At this point, the bad data is in the DB and will give you problems every time you try to use it.

Store data as the most appropriate datatype, not the one which is easiest for you! It saves a heck of a lot of time and effort later.

And the use of string storage implies that you also concatenate strings to form your SQL commands - which is very, very dangerous as it allows the user to perform SQL Injection attacks which can damage or destroy your database. Always use parametrised queries!
 
Share this answer
 
Comments
Slacker007 13-Apr-15 11:40am    
Sound advice.
jaylisto 14-Apr-15 1:28am    
thanks for your advise.
what revision do you suggest on my code?
how do i properly parse at the point of input.?
later i will make a stored procedure to avoid injection . as of now, im having difficulties with daytime.
You can try DateTime.Parse() method with all its overloads.

DateTime.Parse Method[^]

DateTime.TryParse can be an option, too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900