Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 13th, 2003, 10:20 AM
Registered User
 
Join Date: Jul 2003
Location: Milan, , Italy.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to convert a string in DateTime

In a windowsform I have three combo box that should help the user to select
the day, the month and the year of his/her birthdate.
I build a string date and I try to convert the string in DateTime in this way :

string day = comboBox1.SelectedItem.ToString();
string month = comboBox2.SelectedItem.ToString();
string year = comboBox3.SelectedItem.ToString();
string date = day+ "/" + month + "/" + year;
DateTime dtDate = Convert.ToDateTime(date);
miaPersona.DataNascita = dtDate;

Then I call a method that perform the Insert in my db table, but I have this error message :

"
[Classe cPersona.cs Metodo Inserisciti : ] Classe DBService.cs Metodo ExecuteNonQuery :
INSERT INTO Anagrafica (PK_Cod_Fiscale,Nome,Cognome,Data_Nascita)
VALUES ('nuksomncsddesllo','elena','rossi','06/07/1925 0.00.00')
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated."

Here is my Insert method:

Method Inserisciti

public void Inserisciti(string SqlName, string DBName, string UserID, string Pwd)
{
    myDB = new DBService(SqlName);
    string input="";

    try
    {
       myDB.Connetti(DBName,UserID,Pwd);
           input="INSERT INTO Anagrafica " + "(PK_Cod_Fiscale,Nome,Cognome,Data_Nascita)"
+ "VALUES ( ' " + pCodice + " ',' " + pNome + " ',' " + pCognome + " ',' " + pDataNascita + "')";

        myDB.ExecuteNonQuery(input);

    }
    catch(Exception exc2)
    {
        throw(new Exception("Classe cPersona.cs Metodo Inserisciti : ",exc2));
    }
    finally
    {
        myDB.Disconnetti();
    }
}

And here is the method ExecuteNonQuery :

public void ExecuteNonQuery(string sqlString)
{
    try
    {
        SqlCommand myCommand = new SqlCommand(sqlString,conn);
        myCommand.ExecuteNonQuery();
    }
    catch(Exception e1)
    {
        throw(new Exception("Classe DBService.cs Metodo ExecuteNonQuery : " + sqlString + " ", e1));
    }

}


The data type in my date field is datetime. Where is my mistake?
I think I have not understood how the Convert.ToDateTime works.
How a date is formatted also depends on the regional settings of the computer,
in which way I can check the right format?





Reply With Quote
  #2 (permalink)  
Old July 13th, 2003, 10:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'd suggest a few things:

1. Check that you had valid values for the year, month and day
2. Don't use possible reserved keywords like date. From memory this returns a date, or at least in VBScript is does.
3. Build your date string as yyyy/mm/dd.

regards
David Cameron
Reply With Quote
  #3 (permalink)  
Old July 14th, 2003, 03:32 AM
Registered User
 
Join Date: Jul 2003
Location: Milan, , Italy.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ciao David,
I have already tried to pass the string as yyyy/mm/dd, but I always have the same error.
Also the name of the variable date is different, and it's not a reserved keywords.
How can I check the values for the year, month and day?
Thank you
Ciao
Elena






Reply With Quote
  #4 (permalink)  
Old July 14th, 2003, 03:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Sydney, NSW, Australia.
Posts: 215
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You check them for reasoable values. -2000 is not a reasonable value for a year, 40000 is not a reasonable value for a day and 13 is not a reasonable value for a month. Try using IsDate.

regards
David Cameron
Reply With Quote
  #5 (permalink)  
Old July 15th, 2003, 06:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Elena,

Just a thought, but if you replace your drop-downs with calendar controls you automatically get 'reasonable' date ranges, and ought to circumvent the Locale issues, too.

Chris

There are two secrets to success in this world:
1. Never tell everything you know
Reply With Quote
  #6 (permalink)  
Old March 29th, 2006, 09:46 AM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use DateTime.Parse(string);

This should work

Thanks
Rajesh
Reply With Quote
  #7 (permalink)  
Old March 29th, 2006, 06:59 PM
Friend of Wrox
Points: 6,664, Level: 34
Points: 6,664, Level: 34 Points: 6,664, Level: 34 Points: 6,664, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

FYI I never give free text fields for dates. Take The advice of chris, use a date picker and place some client side validation iside the imput area not allowing onFocus, onClick etc...

You use what ever date format you want to. Yes yyyy/mm/dd is the international format but not for SQL server. This post not only has your solution, functions and code - but input from some of the forums smartest cookies.

http://p2p.wrox.com/topic.asp?TOPIC_...s=date,formats

Wind is your friend
Matt
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert String to DateTime SimoNWellborne C# 3 October 11th, 2014 03:24 AM
convert string to datetime in VB.net lisabb ASP.NET 2.0 Basics 3 June 19th, 2007 07:52 AM
Convert string to datetime nkovacevic General .NET 4 April 5th, 2005 12:57 PM
Convert DateTime rgerald SQL Server 2000 3 October 5th, 2004 10:00 PM
How to convert a string in DateTime elena ADO.NET 3 July 24th, 2003 05:31 PM



All times are GMT -4. The time now is 04:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.