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?
|