Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
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 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 14th, 2003, 02:11 PM
Registered User
 
Join Date: Jul 2003
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?

 
Old July 15th, 2003, 02:08 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old July 17th, 2003, 07:21 PM
Registered User
 
Join Date: Jun 2003
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.
 
Old July 24th, 2003, 04:31 PM
Authorized User
 
Join Date: Jun 2003
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






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





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