我试图将数据插入到Datetime类型的字段中,但是我使用的控件是textBox,所以我转换了datetime值,但是在尝试插入以下消息时出现:"输入字符串为格式不正确.".一世 想帮助我,代码片段如下:
I was trying to insert data into a field of type Datetime, but the control that I used was a textBox, I converted the datetime value, but when trying to insert the following message appears: "Input string was not in the correct format.". I wanted to help me, the code snippet is as follows:
da.InsertQuery(this.ProjetoID,int.Parse(contato.Text),this.descricao.Text,this.dataInicio.Value,this.dataFim.Value,DateTime.Parse(horaInicio.Text),DateTime .Parse(horaFim.Text));
da.InsertQuery(this.ProjetoID,int.Parse(contato.Text), this.descricao.Text, this.dataInicio.Value, this.dataFim.Value, DateTime.Parse(horaInicio.Text), DateTime.Parse(horaFim.Text));
谢谢; D
推荐答案
在同一行中进行所有转换会使代码更紧凑,但更难调试.要找出导致错误的字段,请尝试分别进行一次转换,以找出问题所在.另外,您可以使用TryParse 避免转换失败时引发错误的方法.您不想在完成的程序中包含一个消息框,但现在尝试尝试找出问题所在:
Doing all the conversions in the same line makes the code more compact, but harder to debug. To figure out which field is causing the error, try doing the conversions separately, one-by-one, to find out where the problem is. Also, you can use the TryParse method to avoid throwing an error if a conversion fails. You wouldn't want to include a message box in a finished program, but try this to figure out for now what is wrong:
DateTime dtHoraInicio; DateTime dtHoraFim; if (!DateTime.TryParse(horaInicio.Text, out dtHoraInicio)) { MessageBox.Show("'" + horaInicio.Text + "' could not be converted to a date."); return; } if (!DateTime.TryParse(horaFim.Text, out dtHoraFim)) { MessageBox.Show("'" + horaFim.Text + "' could not be converted to a date."); return; }
更多推荐
输入的字符串格式不正确.
发布评论