从字符串转换日期时间时转换失败 [英] Conversion failed when converting datetime from character string

查看:81
本文介绍了从字符串转换日期时间时转换失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面设计

<pre lang="xml"><td>

                         <asp:TextBox ID="txtDateOfBirth" runat="server" ></asp:TextBox>



                         <cc1:CalendarExtender ID="txtDateOfBirth_CalendarExtender" runat="server"

                             Enabled="True" TargetControlID="txtDateOfBirth">

                         </cc1:CalendarExtender>



                     </td>



隐藏代码



Code Behind

protected void btnsave_Click(object sender, EventArgs e)

    {

     Admin aad = new Admin();

aad.addProfile(txtDateOfBirth.Text, Convert.ToInt32(txtAge.Text.Trim());


}
方法


}
Method

public void addProfile(datetime DatyeOfBirth)
{
 SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("sp_addProfile", con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.Parameters.AddWithValue("@DateofBirth",DateOfBirth);
<pre lang="cs">cmd.ExecuteNonQuery();
        con.Close();
    }



}

错误:将failde Datetime转换为char字符串,请解决此问题

在表格中,将DOB字段作为Datetime



}

Error:Converting failde Datetime to char string pls solbe this

In the table i''ve the DOB field as Datetime

推荐答案

尝试一下,可能对您有帮助

try this, may it will help you

using Globalization;

CultureInfo ci=new CultureInfo("gu-IN");

public void addProfile(datetime DatyeOfBirth)
{
 SqlConnection con = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("sp_addProfile", con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.Parameters.AddWithValue("@DateofBirth",Datetime.ParseExact(DateOfBirth,ci));
<pre lang="cs">cmd.ExecuteNonQuery();
        con.Close();
    }




谢谢




Thanks


这篇关于从字符串转换日期时间时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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