[英]Convert Date type C#
I am Creating A Web app I am using a textbox and textbox calender extender to get the date enter by the user, I want Date in this ("dd-MM-yyyy")
format,我正在创建一个 Web 应用程序我正在使用文本框和文本框日历扩展器来获取用户输入的日期,我想要这种("dd-MM-yyyy")
格式的日期,
but I am getting error但我收到错误
(Arithmetic overflow error converting expression to data type datetime.) (将表达式转换为数据类型日期时间时出现算术溢出错误。)
By Default the format of the date is ("yyyy-MM-dd")
this默认情况下,日期的格式是("yyyy-MM-dd")
这个
protected void txttodate_TextChanged(object sender, EventArgs e)
{
string com = Convert.ToString(Session["radio"]);
string com1 = Convert.ToString(Session["mysession"]);
Session["drd3"] = com.ToString();
Session["lblname"] = com1.ToString();
SqlCommand cmdtr = new SqlCommand("select empname from trainerdetails where trid='"+com1.ToString()+"'",con);
con.Open();
SqlDataReader dr = cmdtr.ExecuteReader();
while (dr.Read())
{
lblempname.Text = dr["empname"].ToString();
}
GridView1.Visible = true;
connectionstr = WebConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
con = new SqlConnection(connectionstr);
SqlCommand cmd = new SqlCommand("csuvdaterange");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
SqlParameter[] param =
{
new SqlParameter("@logintype",com.ToString()),
new SqlParameter("@name",lblempname.Text),
new SqlParameter("@datefrm",txtfrmdate.Text),
new SqlParameter("@dateto",txttodate.Text)
};
cmd.Parameters.AddRange(param);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
This Is How My Backend Code looks这就是我的后端代码的样子
<asp:TextBox ID="txtfrmdate" Visible="false" runat="server" placeholder="From" style="margin-left:10px; margin-top:8px;" Height="30px" Width="200px"></asp:TextBox>
<asp:CalendarExtender ID="calenderfromextend" TargetControlID="txtfrmdate" runat="server" Format="dd-MM-yyyy"></asp:CalendarExtender>
<asp:TextBox ID="txttodate" placeholder="To" Visible="false" runat="server" style="margin-left:10px; margin-top:8px;" AutoPostBack="true" OnTextChanged="txttodate_TextChanged" Height="30px" Width="200px"></asp:TextBox>
<asp:CalendarExtender ID="calendertoextend" TargetControlID="txttodate" runat="server" Format="dd-MM-yyyy"></asp:CalendarExtender>
My Textboxes and calender extender我的文本框和日历扩展器
After Implementing Jon's suggestions on using Parameterised SQL , using using
statements and if you are sure that the dates entered in TextBox
are in format " yyyy-MM-dd
", you may use this code to change the format string:在实现 Jon 的关于使用参数化 SQL的建议后,使用using
语句并且如果您确定在TextBox
中输入的日期格式为“ yyyy-MM-dd
”,您可以使用此代码来更改格式字符串:
SqlParameter[] param =
{
new SqlParameter("@logintype",com.ToString()),
new SqlParameter("@name",lblempname.Text),
new SqlParameter("@datefrm",DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd")),
new SqlParameter("@dateto",DateTime.ParseExact(txttodate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToString("yyyy-MM-dd"))
};
First parse your textbox to a DateTime object by DateTime.ParseExact
首先通过DateTime.ParseExact
将您的文本框解析为 DateTime 对象
var dateFrom = DateTime.ParseExact(txtfrmdate.Text, "dd-MM-yyyy", null);
var dateTo = DateTime.ParseExact(txttodate.Text, "dd-MM-yyyy", null);
After that, replace in your SqlParameter
:之后,替换你的SqlParameter
:
SqlParameter[] param =
{
new SqlParameter("@logintype",com.ToString()),
new SqlParameter("@name",lblempname.Text),
new SqlParameter("@datefrm",SqlDbType.DateTime) { Value = dateFrom },
new SqlParameter("@dateto", SqlDbType.DateTime) { Value = dateTo }
};
String.Format("{0:MM/dd/yyyy}", dt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.