如何通过C#ASP.net存储在SQL Server从文本框中日期 [英] How to store date from text box in SQL Server through C# ASP.net

查看:229
本文介绍了如何通过C#ASP.net存储在SQL Server从文本框中日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦,同时通过C#asp.net存储在SQL Server DB日期,在线

I am having trouble while storing date in sql server DB through C# asp.net, online

我用的ASP文件的asp文本框和ASP压延扩展到获得用户的日期,

I have used asp Text box and asp calender extender in ASP file to get date from user,

 <asp:TextBox runat="server" ID="txt_date"></asp:TextBox>
<asp:CalendarExtender runat="server" ID="cal_date" TargetControlID="txt_date"></asp:CalendarExtender>

$ C $隐藏文件C是假设连接和命令的声明和初始化,

code behind file is, assume connection and command are declared and initialized ,

mycom = new SqlCommand("insert into mytable(dtCol1) values('"+Convert.ToDateTime(txt_dob.Text).ToString("dd-MMM-yyyy") + "')", mycon);mycom.ExecuteNonQuery();

问题是,当我选择日期不到它的作品完美每月的12,
但是,当日期/天,大于12个月的任何它给错误的,

Problem is, when I select date less than 12 of any month it works perfect, but when date/day is greater than 12 of any month it gives error,

Exception Details: System.FormatException: String was not recognized as a valid DateTime.

我已经试过的ToString(DD-MMM-YYYY)

I have tried all combinations of .ToString("dd-MMM-yyyy")

请帮助
在此先感谢

Please Help thanks in advance

推荐答案

试试这个

CultureInfo provider = CultureInfo.InvariantCulture;
System.Globalization.DateTimeStyles style = DateTimeStyles.None;
DateTime dt;
DateTime.TryParseExact(txt_dob.Text, "m-d-yyyy", provider, style, out dt);
mycom = new SqlCommand("insert into mytable(dtCol1) values(@datevalue)", mycon);
cmd.Parameters.Add("@datevalue",SqlDbType.DateTime).Value =dt;
mycom.ExecuteNonQuery();

这篇关于如何通过C#ASP.net存储在SQL Server从文本框中日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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