Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 Search this Thread Display Modes
  #1 (permalink)  
Old July 14th, 2003, 02:11 PM
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

Ciao,
I have already posted this issue in the Database forum, anyway my problem is that 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 15th, 2003, 02:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Wien, Wien, Austria.
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to NotNowJohn
Default

If you want to display the date in some other format (par example to en-US) u can use this:
using System.Globalization;
CultureInfo ci=new CultureInfo("en-US");
string mod_dt=dt.ToString(ci);


...but the Soon is eclipsed by the Moon
Reply With Quote
  #3 (permalink)  
Old July 17th, 2003, 07:21 PM
Registered User
 
Join Date: Jun 2003
Location: Pantai Remis, Perak, Malaysia.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to samlow82 Send a message via Yahoo to samlow82
Default

Dim DateAndTimeStarted As String

DateAndTimeStarted = txtDateStarted.Text & " " & txtHourStarted.Text & ":" & txtMinutesStarted.Text & ":" & txtSecondsStarted.Text & " " & drpTimeStartedAMorPM.SelectedItem.Text

rowNew = dsEventLog.ELTS_EVENT_LOG.NewELTS_EVENT_LOGRow
rowNew.DATE_START = DateAndTimeStarted
dsEventLog.ELTS_EVENT_LOG.AddELTS_EVENT_LOGRow(row New)


It works pretty well.Try it out.
Reply With Quote
  #4 (permalink)  
Old July 24th, 2003, 04:31 PM
Authorized User
 
Join Date: Jun 2003
Location: Crowland, Lincs, United Kingdom.
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can use Parse, as long as your date string is correct (incl your country style, as your Windows OS will take care of that) use,
string day = comboBox1.SelectedItem.ToString();
string month = comboBox2.SelectedItem.ToString();
string year = comboBox3.SelectedItem.ToString();
string date = day+ "/" + month + "/" + year;
DateTime dtDate=DateTime.Parse(date);

Good luck
Kasie

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 02:24 AM
convert string to datetime in VB.net lisabb ASP.NET 2.0 Basics 3 June 19th, 2007 06:52 AM
How to convert a string in DateTime elena SQL Language 6 March 29th, 2006 06:59 PM
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



All times are GMT -4. The time now is 01:22 PM.


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