I';我正在尝试将我的表单插入SQL Server,但我收到了这个错误“;从字符串“”转换日期和/或时间时转换失败
本文关键字:转换 字符串 错误 失败 时间 日期 我的 表单 Server 插入 SQL | 更新日期: 2023-09-27 18:12:51
从字符串转换日期和/或时间时转换失败
我正在使用AjaxToolkit进行约会。我的代码如下:
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = " Server=localhost''SQLExpress; Database=ins; Trusted_Connection =True;";
con.Open();
CultureInfo cultureInfo = new CultureInfo("en-US");
SqlCommand cmd = new SqlCommand("Insert Into damage(Company,DocType,PolicyNumber,DamageType,IncidentLocation,Claiment,DateOfLoss,DateOfNotificationDepartmant,DateOfNotificationtoAgency,OutstandingLoss,ClaimSummary,IncidentReferenceNo,RecordDatebyInsurer,DateofSurvey,IssueDateofSurveyorsDamageReport,TotalIncurred,Indemnity,Deductible,DateofIndemnityPayment,Status,PerformanceRatingofInsuranceDepartmant,PerformanceRatingofAgency,PerformanceRatingofInsuranceCompany1,PerformanceRatingofInsuranceCompany2,TotalLeadTime,LossRatio,IncidentFormNo) VALUES (@Company,@DocType,@PolicyNumber,@DamageType,@IncidentLocation,@Claiment,@DateOfLoss,@DateOfNotificationDepartmant,@DateOfNotificationtoAgency,@OutstandingLoss,@ClaimSummary,@IncidentReferenceNo,@RecordDatebyInsurer,@DateofSurvey,@IssueDateofSurveyorsDamageReport,@TotalIncurred,@Indemnity,@Deductible,@DateofIndemnityPayment,@Status,@PerformanceRatingofInsuranceDepartmant,@PerformanceRatingofAgency,@PerformanceRatingofInsuranceCompany1,@PerformanceRatingofInsuranceCompany2,@TotalLeadTime,@LossRatio,@IncidentFormNo)", con);
cmd.Parameters.AddWithValue("@Company", TextBox1.Text);
cmd.Parameters.AddWithValue("@DocType", TextBox2.Text);
cmd.Parameters.AddWithValue("@PolicyNumber", TextBox3.Text);
cmd.Parameters.AddWithValue("@DamageType", TextBox4.Text);
cmd.Parameters.AddWithValue("@IncidentLocation", TextBox5.Text);
cmd.Parameters.AddWithValue("@Claiment", TextBox6.Text);
//DateTime date = DateTime.ParseExact(TextBox7.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
cmd.Parameters.AddWithValue("@DateOfLoss", DateTime.ParseExact(TextBox7.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@DateOfNotificationDepartmant", DateTime.ParseExact(TextBox8.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@DateOfNotificationtoAgency", DateTime.ParseExact(TextBox9.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@OutstandingLoss", TextBox10.Text);
cmd.Parameters.AddWithValue("@ClaimSummary", TextBox11.Text);
cmd.Parameters.AddWithValue("@IncidentReferenceNo", TextBox12.Text);
cmd.Parameters.AddWithValue("@RecordDatebyInsurer", DateTime.ParseExact(TextBox13.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@DateofSurvey", DateTime.ParseExact(TextBox14.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@IssueDateofSurveyorsDamageReport", DateTime.ParseExact(TextBox15.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@TotalIncurred", TextBox16.Text);
cmd.Parameters.AddWithValue("@Indemnity", TextBox17.Text);
cmd.Parameters.AddWithValue("@Deductible", TextBox18.Text);
cmd.Parameters.AddWithValue("@DateofIndemnityPayment", DateTime.ParseExact(TextBox19.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture));
cmd.Parameters.AddWithValue("@Status", TextBox20.Text);
cmd.Parameters.AddWithValue("@PerformanceRatingofInsuranceDepartmant", TextBox21.Text);
cmd.Parameters.AddWithValue("@PerformanceRatingofAgency", TextBox22.Text);
cmd.Parameters.AddWithValue("@PerformanceRatingofInsuranceCompany1", TextBox23.Text);
cmd.Parameters.AddWithValue("@PerformanceRatingofInsuranceCompany2", TextBox24.Text);
cmd.Parameters.AddWithValue("@TotalLeadTime", TextBox25.Text);
cmd.Parameters.AddWithValue("@LossRatio", TextBox26.Text);
cmd.Parameters.AddWithValue("@IncidentFormNo", TextBox27.Text);
int rowsaffected = cmd.ExecuteNonQuery();
if (rowsaffected == 1)
{
Label1.Text = "Damage ticket has been delivered.";
}
else
{
Label1.Text = "Damage ticket has been not delivered.";
}
con.Close();
}
catch (Exception a)
{
Console.WriteLine("{0} Exception caught.", a);
}
当我调试Textbox7.的值时,文本显示在addwatch上,如:2014年7月14日
Staacktrace如下所示:
{System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting date and/or time from character string.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Test.WebForm2.Button2_Click(Object sender, EventArgs e) in c:'Users'HASAN'Documents'Visual Studio 2013'Projects'Test'Test'New Damage'WebForm2.aspx.cs:line 65
ClientConnectionId:90c6e096-88e8-4634-9d40-8bdfa5474292}
您的问题在这里:
DateTime.ParseExact(TextBox7.Text, "m/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)
m
表示分钟,M
表示月。因此,将格式字符串更改为MM/dd/yyyy