Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2003, 09:20 AM
Registered User
 
Join Date: Jul 2003
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?





 
Old July 13th, 2003, 09:17 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old July 14th, 2003, 02:32 AM
Registered User
 
Join Date: Jul 2003
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






 
Old July 14th, 2003, 02:35 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old July 15th, 2003, 05:39 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old March 29th, 2006, 09:46 AM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use DateTime.Parse(string);

This should work

Thanks
Rajesh
 
Old March 29th, 2006, 06:59 PM
Friend of Wrox
 
Join Date: Jan 2004
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.