字符串未被识别为有效日期时间

时间:2021-05-10 10:33:35

I have 2 textboxes where I have to add the Joining Date and Leaving Date.

我有2个文本框,我必须添加加入日期和离开日期。

Now When I add the joining date and leave the Leaving date blank and submit the form I get error as

现在,当我添加加入日期并将Leaving日期留空并提交表单时,我收到错误

String was not recognized as Valid Datetime

字符串未被识别为有效日期时间

Here is my code for

这是我的代码

cmd1.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = Convert.ToDateTime(txtdol.Text);

I dont know why this is giving error. Please help

我不知道为什么这会给出错误。请帮忙

UPDATE:

Textbox code:-

 <asp:TextBox ID="txtdol" CssClass="form-control" runat="server" ValidationGroup="AddNew" Enabled="true"></asp:TextBox>

5 个解决方案

#1


0  

Ensure you enter date in valid format in the textbox. best option is to use a date validator for the textbox and if the date formate entered is wrong you can display validation error rather than RTE.

确保在文本框中以有效格式输入日期。最好的选择是为文本框使用日期验证器,如果输入的日期格式错误,则可以显示验证错误而不是RTE。

here is the question for validation:

这是验证的问题:

Date validation for a textbox

文本框的日期验证

here is the available date format from MSDN:

这是MSDN的可用日期格式:

https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

#2


0  

of course you get error for that . the error happening in

当然你会得到错误。发生的错误

Convert.ToDateTime(txtdol.Text);

you are trying to convert something that is null , its like you say to compiler to compile this :

你试图转换null的东西,就像你说编译器编译它:

Convert.ToDateTime("");

it's emptry string and cannot be converted .

它是emptry字符串,无法转换。

here are some options that you can consider of :

以下是您可以考虑的一些选项:

  1. Check if the textbox is not empty and the provided date is valid (client or server side)
  2. 检查文本框是否为空并且提供的日期是否有效(客户端或服务器端)

  3. use DateTimePicker (like jquery ui datetimepicker)
  4. 使用DateTimePicker(如jquery ui datetimepicker)

  5. use this code instead of that at the end of that line

    使用此代码而不是该行末尾的代码

    String.IsNullOrEmpty(txtdol.Text) ? DateTime.Now : Convert.ToDateTime(txtdol.Text)

    String.IsNullOrEmpty(txtdol.Text)? DateTime.Now:Convert.ToDateTime(txtdol.Text)

if txtdol is null or empty it return the current datetime otherwise it convert the txtdol to datetime and return it .

如果txtdol为null或为空,则返回当前日期时间,否则将txtdol转换为datetime并返回它。

#3


0  

This error occures due to Invalid date format (empty or wrong format) enterd in textbox to fix the issue modify the code as below , use DateTime.ParsExact

由于在文本框中输入了无效的日期格式(空格或格式错误)以修复问题修改代码如下所示,发生此错误,请使用DateTime.ParsExact

cmd1.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = DateTime.ParsExact(txtdol.Text,"dd/MM/yyyy",System.Globalization.DateTimeStyles.AllowInnerWhite);

the second paramater can be changed to an array of format also.

第二个参数也可以更改为一个格式数组。

Change the code as per your need

根据您的需要更改代码

#4


0  

Good way to input date to Textbox by user is using : 1- MaskedTextBox or 2- DateTimePicker. Or if you want using textbox, you can use this code also to check empty values.

用户输入日期到文本框的好方法是使用:1- MaskedTextBox或2- DateTimePicker。或者,如果您想使用文本框,也可以使用此代码来检查空值。

 DateTime? nullDateTime = null;
        cmd1.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = string.IsNullOrEmpty(txtdol.Text.Trim()) ? nullDateTime : Convert.ToDateTime(txtdol.Text.Trim())

and for validation textbox check this function:

并为验证文本框检查此功能:

private bool validateDate(TextBox arg1)
        {
            DateTime dt;
            bool valid1 = DateTime.TryParseExact(arg1.Text.Trim(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dt);
            if (!string.IsNullOrEmpty(arg1.Text.Trim()) )
            {
                MessageBox.Show("Format Date is wrong! Currect format is(dd/MM/yyyy)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                arg1.Focus();
                return false;
            }
            return true;
        }

#5


0  

After getting so much errors, Did like this

在得到这么多错误之后,就像这样

 if (txtdol.Text == null || txtdol.Text == string.Empty)
                        {
                            cmd.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = Convert.ToDateTime(txtdol.Text);
                        }

And it worked..!!

它工作.. !!

#1


0  

Ensure you enter date in valid format in the textbox. best option is to use a date validator for the textbox and if the date formate entered is wrong you can display validation error rather than RTE.

确保在文本框中以有效格式输入日期。最好的选择是为文本框使用日期验证器,如果输入的日期格式错误,则可以显示验证错误而不是RTE。

here is the question for validation:

这是验证的问题:

Date validation for a textbox

文本框的日期验证

here is the available date format from MSDN:

这是MSDN的可用日期格式:

https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

#2


0  

of course you get error for that . the error happening in

当然你会得到错误。发生的错误

Convert.ToDateTime(txtdol.Text);

you are trying to convert something that is null , its like you say to compiler to compile this :

你试图转换null的东西,就像你说编译器编译它:

Convert.ToDateTime("");

it's emptry string and cannot be converted .

它是emptry字符串,无法转换。

here are some options that you can consider of :

以下是您可以考虑的一些选项:

  1. Check if the textbox is not empty and the provided date is valid (client or server side)
  2. 检查文本框是否为空并且提供的日期是否有效(客户端或服务器端)

  3. use DateTimePicker (like jquery ui datetimepicker)
  4. 使用DateTimePicker(如jquery ui datetimepicker)

  5. use this code instead of that at the end of that line

    使用此代码而不是该行末尾的代码

    String.IsNullOrEmpty(txtdol.Text) ? DateTime.Now : Convert.ToDateTime(txtdol.Text)

    String.IsNullOrEmpty(txtdol.Text)? DateTime.Now:Convert.ToDateTime(txtdol.Text)

if txtdol is null or empty it return the current datetime otherwise it convert the txtdol to datetime and return it .

如果txtdol为null或为空,则返回当前日期时间,否则将txtdol转换为datetime并返回它。

#3


0  

This error occures due to Invalid date format (empty or wrong format) enterd in textbox to fix the issue modify the code as below , use DateTime.ParsExact

由于在文本框中输入了无效的日期格式(空格或格式错误)以修复问题修改代码如下所示,发生此错误,请使用DateTime.ParsExact

cmd1.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = DateTime.ParsExact(txtdol.Text,"dd/MM/yyyy",System.Globalization.DateTimeStyles.AllowInnerWhite);

the second paramater can be changed to an array of format also.

第二个参数也可以更改为一个格式数组。

Change the code as per your need

根据您的需要更改代码

#4


0  

Good way to input date to Textbox by user is using : 1- MaskedTextBox or 2- DateTimePicker. Or if you want using textbox, you can use this code also to check empty values.

用户输入日期到文本框的好方法是使用:1- MaskedTextBox或2- DateTimePicker。或者,如果您想使用文本框,也可以使用此代码来检查空值。

 DateTime? nullDateTime = null;
        cmd1.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = string.IsNullOrEmpty(txtdol.Text.Trim()) ? nullDateTime : Convert.ToDateTime(txtdol.Text.Trim())

and for validation textbox check this function:

并为验证文本框检查此功能:

private bool validateDate(TextBox arg1)
        {
            DateTime dt;
            bool valid1 = DateTime.TryParseExact(arg1.Text.Trim(), "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dt);
            if (!string.IsNullOrEmpty(arg1.Text.Trim()) )
            {
                MessageBox.Show("Format Date is wrong! Currect format is(dd/MM/yyyy)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                arg1.Focus();
                return false;
            }
            return true;
        }

#5


0  

After getting so much errors, Did like this

在得到这么多错误之后,就像这样

 if (txtdol.Text == null || txtdol.Text == string.Empty)
                        {
                            cmd.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters.Add("@leaving_date", SqlDbType.DateTime).Value = Convert.ToDateTime(txtdol.Text);
                        }

And it worked..!!

它工作.. !!