简体   繁体   English

如何解决无法将参数值从字符串转换为asp.net中的DateTime?

[英]How To fix Failed to convert parameter value from a String to a DateTime in asp.net?

I want to insert all the data in the form to the database named Equipment. 我想将表单中的所有数据插入名为Equipment的数据库中。 However, when I click on the save button it shows error "Failed to convert parameter value from a String to a DateTime." 但是,当我单击保存按钮时,它显示错误“无法将参数值从字符串转换为DateTime”。

I have set the data type for certain column in date time type. 我已经为日期时间类型中的某些列设置了数据类型。 I put the calendar in the text box, so by clicking on the text box, it will pop up the calendar. 我将日历放在文本框中,因此通过单击文本框,它将弹出日历。 The user can choose the date. 用户可以选择日期。 The calendar that has been creating using javascript. 已使用javascript创建的日历。

Anyone know how to fix this problem? 有人知道如何解决此问题吗?

The form and the calendar pop up 表格和日历弹出

The code for save button: 保存按钮的代码:

string connectionstring = "Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
    using (SqlConnection connection = new SqlConnection(connectionstring))
    {

        string sql = @"INSERT Into [Equipment]([OwnerID],[SubjecttoCal],[Model],[Option],[EquipmentID],[SerialNumber],[Description],[Location],
                            [DueDate],[EquipmentWithdraworRemarks],[NCRorOOTHistory],[LastOOTissuanceDate],
                            [AvailableinSapphire],[ResponsiblePerson],[CalibrationOption],[CalibrationSourceorLab],
                            [YearofManufacturing],[ManufacturerorVendor],[CalibrationCost],[AssetNo],[CalibrationTAT],[SendInDate],[Status])
                           ";

        using (SqlCommand command = new SqlCommand(sql, connection))
        {
            command.Parameters.Add("@OwnerID", SqlDbType.NVarChar).Value = TextBox1.Text;
            command.Parameters.Add("@SubjecttoCal", SqlDbType.NVarChar).Value = TextBox2.Text;
            command.Parameters.Add("@Model", SqlDbType.NVarChar).Value = TextBox3.Text;
            command.Parameters.Add("@Option", SqlDbType.NVarChar).Value = TextBox4.Text;
            command.Parameters.Add("@EquipmentID", SqlDbType.NVarChar).Value = TextBox5.Text;
            command.Parameters.Add("@SerialNumber", SqlDbType.NVarChar).Value = TextBox6.Text;
            command.Parameters.Add("@Description", SqlDbType.NVarChar).Value = TextBox7.Text;
            command.Parameters.Add("@Location", SqlDbType.NVarChar).Value = DropDownList1.Text;
            command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = TextBox8.Text;
            command.Parameters.Add("@EquipmentWithdraworRemarks", SqlDbType.NVarChar).Value = TextBox10.Text;
            command.Parameters.Add("@NCRorOOTHistory", SqlDbType.NVarChar).Value = TextBox11.Text;
            command.Parameters.Add("@LastOOTissuanceDate", SqlDbType.DateTime).Value = TextBox12.Text;

            command.Parameters.Add("@AvailableinSapphire", SqlDbType.NVarChar).Value = TextBox13.Text;
            command.Parameters.Add("@ResponsiblePerson", SqlDbType.NVarChar).Value = TextBox14.Text;
            command.Parameters.Add("@CalibrationOption", SqlDbType.NVarChar).Value = TextBox15.Text;
            command.Parameters.Add("@CalibrationSourceorLab", SqlDbType.NVarChar).Value = TextBox16.Text;

            command.Parameters.Add("@YearofManufacturing", SqlDbType.NVarChar).Value = TextBox17.Text;
            command.Parameters.Add("@ManufacturerorVendor", SqlDbType.NVarChar).Value = TextBox18.Text;
            command.Parameters.Add("@CalibrationCost", SqlDbType.NVarChar).Value = TextBox19.Text;
            command.Parameters.Add("@AssetNo", SqlDbType.NVarChar).Value = TextBox20.Text;

            command.Parameters.Add("@CalibrationTAT", SqlDbType.NVarChar).Value = TextBox21.Text;
            command.Parameters.Add("@SendInDate", SqlDbType.DateTime).Value = TextBox22.Text;
            command.Parameters.Add("@Status", SqlDbType.NVarChar).Value = DropDownList2.Text;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Your data have been saved')", true);



        }
    }

The javacript code: javacript代码:

<script type="text/javascript">
    $(function () {
        $("[id$=TextBox8]").datepicker({
            dateFormat: 'dd/mm/yy', changeYear: true
        });
        $("[id$=TextBox12]").datepicker({
            dateFormat: 'dd/mm/yy', changeYear: true
        });
        $("[id$=TextBox17]").datepicker({
            dateFormat: 'dd/mm/yy', changeYear: true
        });
        $("[id$=TextBox22]").datepicker({
            dateFormat: 'dd/mm/yy', changeYear: true
        });
    });
</script>

You need to convert your date string into datetime format. 您需要将日期字符串转换为日期时间格式。 For eg: 例如:

DateTime oDate = DateTime.ParseExact(textboxValue, "MM/dd/yy", null);

//For eg
command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox8.Text, "MM/dd/yy", null);

Btw, I am assuming TextBox8.Text return correct value from your datetime picker. 顺便说一句,我假设TextBox8.Text从您的日期时间选择器返回正确的值。

If it works then just do the same thing with other datetime textboxes 如果可行,则对其他日期时间文本框执行相同操作

As looking at your code, you are taking date in "dd/MM/yyyy" format from the client side in textbox. 在查看代码时,您正在文本框中从客户端获取“ dd / MM / yyyy”格式的日期。 So you need to parse your date considering that format by following snippet. 因此,您需要按照以下代码段分析使用该格式的日期。

DateTime parsedDate = DateTime.ParseExact("22/3/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture);

For your code you need to replace your "textbox.Text" with statement "DateTime.ParseExact(textbox.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)" whereever you have mentiond data type DateTime. 对于您的代码,无论何处提到数据类型为DateTime的地方,都需要用语句“ DateTime.ParseExact(textbox.Text,” dd / MM / yyyy“,CultureInfo.InvariantCulture)”替换“ textbox.Text”。

ie

        command.Parameters.Add("@DueDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox8.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

        command.Parameters.Add("@LastOOTissuanceDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox12.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

        command.Parameters.Add("@SendInDate", SqlDbType.DateTime).Value = DateTime.ParseExact(TextBox22.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM