输入的字符串格式不正确.

编程入门 行业动态 更新时间:2024-10-28 09:28:27
本文介绍了输入的字符串格式不正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图将数据插入到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; }

更多推荐

输入的字符串格式不正确.

本文发布于:2023-06-04 17:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/502161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不正确   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!