字符串未被识别为C#asp.net有效的DateTime [英] String was not recognized as a valid DateTime in C# asp.net

查看:135
本文介绍了字符串未被识别为C#asp.net有效的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导入Excel单元格中的日期值。有二零一三年十月十日格式的单元格值。我想将其转换为datetime数据类型。我的code得到错误字符串未被识别为有效的日期时间

I want to import date value from excel cell. cell value having "10 October 2013" format. 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.

推荐答案

我建议您在构建SQL对象之前,利用DateTime.TryParse方法。确保您有质量的输入有与数据库对话之前。

I recommend the utilizing the DateTime.TryParse method before constructing your SQL objects. Ensure you have quality input before having a conversation with your database.

http://msdn.microsoft。 COM / EN-US /库/ ch92fbc1%28V = vs.110%29.aspx

下面是我自己的code样本为asp.net应用程序

Below is a sample from my own code for an asp.net application

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

这篇关于字符串未被识别为C#asp.net有效的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆