Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do i convert time for sendin to data base. I write code for converting time like this("DateTime time = DateTime.Parse(desiretime.EditValue.ToString());") After run the project the error come like this("The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.")
Posted
Updated 1-May-15 2:05am
v2
Comments
Thomas Daniels 1-May-15 7:09am    
What does desiretime.ToString() look like?
Atta Ur Rahman 118 1-May-15 8:08am    
Sir it is like DateTime.Parse(desiretime.EditValue.ToString()); desir time time is name of time which give to it.
Thomas Daniels 1-May-15 8:10am    
What's its value?
Atta Ur Rahman 118 1-May-15 8:21am    
The error now come is this "An explicit value for the identity column in table 'appointment' can only be specified when a column list is used and IDENTITY_INSERT is ON."
Atta Ur Rahman 118 1-May-15 8:24am    
private void bt_requesting_Click(object sender, EventArgs e)
{
string requestappoint = combo_requstinpersonappoint.SelectedItem.ToString();
string requestpersonname = tb_requestingpersnname.Text.Trim();
string relation = tb_relation.Text.Trim();
string patientname = tb_patntnmae.Text.Trim();
string adres = tb_address.Text.Trim();
string city = tb_city.Text.Trim();
string cell = tb_cell.Text.Trim();
string patienttype = comboBox_patienttype.SelectedItem.ToString();
string apointtype = comboBox_appointtype.SelectedItem.ToString();
DateTime date = DateTime.Parse(desirdate.EditValue.ToString());
DateTime time = DateTime.Parse(desiretime.EditValue.ToString());
string insert = string.Format("insert into appointment values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')", requestappoint, requestpersonname, relation, patientname, adres, city, cell, patienttype, patienttype, desirdate, desiretime);
SqlCommand cmd = new SqlCommand(insert, dbcs.open());
cmd.ExecuteNonQuery();
MessageBox.Show("Request Complete");
}
Sir it is Full Code

1 solution

Use DateTime.TryParse[^] instead of DateTime.Parse. TryParse will only convert if it finds the input to be a valid date.
if (DateTime.TryParse(desiretime, out dateValue)) 
{
   //A date
}
else
{
   //Not a date
}
 
Share this answer
 
v3
Comments
Atta Ur Rahman 118 1-May-15 8:09am    
I enter only time... plz write me only about time not date...
Atta Ur Rahman 118 1-May-15 8:10am    
Sir now this error come to me (An explicit value for the identity column in table 'appointment' can only be specified when a column list is used and IDENTITY_INSERT is ON.)
F-ES Sitecore 1-May-15 8:40am    
This problem is unrelated to your original one, you should really create a new question for it. You're trying to insert a record with an ID when the table is using IDENTITY for its ID column. When you use IDENTITY you don't supply an ID, SQL created one for you. You probably just need to drop the first param from your query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900