Convetting string/text to datetime

R

Randel Bjorkquist

I'm extremely new to C# and the Microsoft Visual Studio IDE. I'm writing a
small web-application that executes a SQL Server stored procedure. For the
most part, I have it working. I am at the point where I now just need to
pass two more fields to the stored procedure, they are both to be date
values.

I have on my web form, two TextBoxes that I am pulling the value from.
However, I have not found a way to convert a string, or text value, to a
date value. What I have found is a DateTime class, but all of its
constructors take numeric values to its parameters and not text. I looked
under the "string" class but didn't see any methods that would help me.

Can someone point me into the right direction? Any small, easy example
would be helpfull.

Thanks,

Randel Bjorkquist
 
L

Ludwig Stuyck

I'm extremely new to C# and the Microsoft Visual Studio IDE. I'm writing a
small web-application that executes a SQL Server stored procedure. For the
most part, I have it working. I am at the point where I now just need to
pass two more fields to the stored procedure, they are both to be date
values.

I have on my web form, two TextBoxes that I am pulling the value from.
However, I have not found a way to convert a string, or text value, to a
date value. What I have found is a DateTime class, but all of its
constructors take numeric values to its parameters and not text. I looked
under the "string" class but didn't see any methods that would help me.

Can someone point me into the right direction? Any small, easy example
would be helpfull.

Thanks,

Randel Bjorkquist

DateTime.Parse(...) will do the trick! Youcan also use
Convert.ToDateTime(...).
 
R

Randel Bjorkquist

Hi Ludwig,

I keep forgetting about the "parse" method.

Thanks for your help,

Randel Bjorkquist
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You could use DateTime.Parse or DateTime.ParseExact , a couple of remarks
though.
1- They throw exceptions if the string is not in the correct format, so you
better use a try block
2- It's a good idea to use also a Calendar web control, this makes it easier
for the users to enter values
3- Use page validators in the textboxes, to make sure that the text is in
fact a correct date expression.


hope this help,
 
R

Randel Bjorkquist

Hey Ignacio,

Thanks for the help. I had just started looking into managing the
exceptions these might throw, when I checked back and saw your message.

Again, thanks,

Randel Bjorkquist
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top