输入的字符串格式不正确

输入的字符串格式不正确

本文介绍了输入的字符串格式不正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

 


    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;
    }


这篇关于输入的字符串格式不正确.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:38