DateTime.Parse字符串转换为DateTime格式等效为DateTime场数据库 [英] DateTime.Parse Converting String to DateTime Format Equals to DateTime Field in Database

查看:140
本文介绍了DateTime.Parse字符串转换为DateTime格式等效为DateTime场数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图YYYY / MM / DD的日期值从一个文本框为datetime,转换时的值是正确的,这是确定,但是当我试着输入了不正确的数据与数据库校验,错误回报字符串未被识别为有效的DateTime。

I am trying to convert a date value of YYYY/MM/DD from a textbox to datetime, when the value is correct it is ok but when I tried to input an incorrect data to check with the database, the error return as String was not recognized as a valid DateTime.

下面是我的code:

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string format = "YYYY-MM-DD HH:MM:SS";
        DateTime birthday = DateTime.Parse(txtBday.Text);
        DataSet ds = new DataSet();
        ds = (newService.checkAccount());
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dRow in ds.Tables[0].Rows)
            {
                string accountNo = dRow["ACCTNO"].ToString();
                DateTime birthDate = DateTime.Parse(dRow["DATEOFBIRTH"].ToString());
                if (accountNo == txtAccountNo.Text.ToString() && birthDate == birthday)
                {
                    lblMessage.Text = "<br>Account Number Exist. You may now proceed with the registration<br><br>";
                    HttpCookie lmsCookie = new HttpCookie("id");
                    lmsCookie.Value = txtAccountNo.Text;
                    Response.Cookies.Add(lmsCookie);
                    Response.Redirect("Step2.aspx");
                }
                else
                {
                    Image2.Visible = false;
                    lblMessage.Text = "<br>Please check your information and try again." + "<br>Be sure you are entering the correct information.For further assistance, call (+632) 404-2790.<br><br>";
                }
            }
        }
    }

例如,我将进入一个数据将在数据库匹配,程序会以其他方式进行,如果我要输入一个不与任何数据库中的现有记录匹配的数据,程序将触发一个错误,字符串未被识别为有效日期时间。

For example I will enter a data that will match in the database, the program will proceed otherwise if I am going to input a data that does not match with any of the existing records in the database, the program will triggers an error, String was not recognized as valid datetime.

推荐答案

在分析失败有原因的其他的不是一个错误,而不是这个,:

When you parsing can fail for a reason other than a bug, instead of this:

DateTime birthDate = DateTime.Parse(dRow["DATEOFBIRTH"].ToString())

(它抛出,你见过一个例外),使用的 DateTime.TryParse

DateTime birthDate;
if (DateTime.TryParse(dRow["DATEOFBIRTH"].ToString(), out birthDate))
{
    // Success case
}
else
{
    // Handle error case
}

我注意到,你不是的使用的你的格式变量,顺便说一句。如果你知道是什么格式将是(和你的问题不同意你的code,顺便说一句),这将是更好地使用 TryParseExact

I note that you're not using your format variable, by the way. If you know what the format will be (and your question disagrees with your code, btw) it would be better to use TryParseExact:

if (DateTime.TryParseExact(dRow["DATEOFBIRTH"].ToString(), "YYYY/MM/dd",
                           CultureInfo.InvariantCulture, DateTimeStyles.None,
                           out birthDate))
...

这篇关于DateTime.Parse字符串转换为DateTime格式等效为DateTime场数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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