简体   繁体   English

在C#asp.net中,字符串未被识别为有效的DateTime

[英]String was not recognized as a valid DateTime in C# asp.net

I want to import date value from excel cell. 我想从Excel单元格导入日期值。 cell value having "10 October 2013" format. 具有“ 2013年10月10日”格式的单元格值。 I want to convert it to datetime data type. 我想将其转换为日期时间数据类型。 My code getting error "string was not recognized as a valid datetime" 我的代码获取错误“字符串未被识别为有效的日期时间”

//code //码

      OleDbCommand olecmd = new OleDbCommand("select * from [Sheet1$]", olecon);


               OleDbDataReader olerdr = olecmd.ExecuteReader();
                 while (olerdr.Read())
                 {
                    deldate = olerdr.GetValue(13).ToString();
                     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["irisdb"].ConnectionString))
                     {
                         con.Open();
                         SqlCommand cmd = new SqlCommand("procdamandrugs", con);
                         cmd.CommandType = CommandType.StoredProcedure;
     DateTime dt = DateTime.ParseExact(deldate, "MM/dd/yyyy", CultureInfo.InvariantCulture);//getting error in this line
                         SqlParameter par9 = new SqlParameter();
                         par9.ParameterName = "@deleffdate";
                         par9.SqlDbType = SqlDbType.DateTime;
                         par9.Value = dt;
                         cmd.Parameters.Add(par9);
 cmd.ExecuteNonQuery();
    }
    }

Do any one help me to solve this issue. 有人帮我解决这个问题。

cell value having "10 October 2013" format. 具有“ 2013年10月10日”格式的单元格值。

You are giving wrong format in ParseExact that does not match with the date string you are passing. 您在ParseExact中输入的格式错误,与您传递的日期字符串不匹配。 You need different format than you gave. 您需要的格式与您给的格式不同。 For day you need dd , for month you need MMMM and for year you need yyyy and you have to give spaces as separator. 对于一天,您需要dd ,对于月份,您需要MMMM ,对于年份,您需要yyyy并且必须给定空格作为分隔符。

It is worth the article Custom Date and Time Format Strings on MSDN for using the string formats for date conversion. 值得使用MSDN上的“ 自定义日期和时间格式字符串”一文 ,以使用字符串格式进行日期转换。

DateTime dt = DateTime.ParseExact(deldate, "dd MMMM yyyy", CultureInfo.InvariantCulture);

I recommend the utilizing the DateTime.TryParse method before constructing your SQL objects. 我建议在构造SQL对象之前利用DateTime.TryParse方法。 Ensure you have quality input before having a conversation with your database. 与数据库进行对话之前,请确保您输入的质量较高。

http://msdn.microsoft.com/en-us/library/ch92fbc1%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-CN/library/ch92fbc1%28v=vs.110%29.aspx

Below is a sample from my own code for an asp.net application 下面是我自己的asp.net应用程序代码示例

    // Validation
    DateTime dtOut_StartDate;
    if (!DateTime.TryParse(txtStartDate.Text, out dtOut_StartDate))
    {
        Message = "Start date is not a valid format.";
        txtStartDate.CssClass = ErrorCssClass.TextBox;
        txtStartDate.Focus();
        return false;
    }

Select Date Time setting from Right lower bottom & Change the format from here...... 从右下方选择日期时间设置,然后从此处更改格式...

在此处输入图片说明

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

相关问题 字符串未被识别为有效日期时间asp.net C# - string was not recognized as a valid datetime asp.net c# 将字符串解析为DateType ASP.NET C#异常-无法将该字符串识别为有效的DateTime - Parse string to DateType ASP.NET C# exception - The string was not recognized as a valid DateTime 字符串未被识别为有效的日期时间,asp.net - String was not recognized as a valid DateTime, asp.net 错误:字符串未被识别为有效的DateTime。 我正在使用带有asp.net和C#Web窗体和SQL数据库的jQuery Datepicker - Error: String was not recognized as a valid DateTime. I'm using jquery datepicker with asp.net and c# web forms and sql databse 发布后出错:字符串未被识别为有效的日期时间 asp.net - Error after publishing : String was not recognized as a valid DateTime asp.net 在asp.net中,不能将字符串识别为有效的DateTime,托管在godaddy上 - String was not recognized as a valid DateTime in asp.net, hosted on godaddy Javascript日期到ASP.NET日期:字符串未被识别为有效的DateTime - Javascript date to ASP.NET date: String was not recognized as a valid DateTime ASP.NET“字符串未被识别为有效的DateTime”。 - ASP.NET “String was not recognized as a valid DateTime.” 无法识别ASP.NET C#中的datetime格式 - datetime format in ASP.NET C# not recognized 使用C#不将字符串识别为有效的DateTime - String was not recognized as a valid DateTime using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM