提问人: 提问时间:2/10/2023 更新时间:2/13/2023 访问量:162
C# ASP.NET - 使用 textmode=datetimelocal 的日期文本框,不将日期选择保存到 SQL Server 数据库中,并且不从 SQL Server 获取日期
C# ASP.NET - date textbox using textmode=datetimelocal not saving date selection into SQL Server database, and not getting date from SQL Server
问:
我正在添加日期文本框,但在单击更新按钮后,它没有将日期选择保存到 SQL Server 数据库中。textmode=datetimelocal
页面加载后,我没有获取日期或无法从 SQL Server 数据库提取日期(通过使用开发服务器模式)。当我在我的本地主机上测试代码时,日期文本框运行良好(可以存储选择日期并可以将日期提取到文本框中)。
HTML格式:
<asp:TextBox ID="TargetDate" runat="server" TextMode="DateTimeLocal" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Update" OnClick="UpdateBtn_Click"/>
C#
if (!Page.IsPostBack)
{
:
:
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
txtDate.Text = sdr["TargetDate"].ToString();
}
protected void UpdateBtn_Click(object sender, EventArgs e)
{
DateTime targetDate = DateTime.Parse(Request.Form[TargetDate.UniqueID]);
con.Open();
SqlCommand cmd = new SqlCommand();
try
{
cmd = new SqlCommand("UPDATE Table SET TargetDate=@TargetDate WHERE CaseID=@CaseID", con);
cmd.Parameters.AddWithValue("@TargetDate", targetDate);
cmd.ExecuteNonQuery();
string message = "You have updated case detail.";
string script = "window.onload = function(){ alert('";
script += message;
script += "')};";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
Response.AddHeader("REFRESH", "2");
Response.Redirect(Request.Url.AbsoluteUri);
}
catch (Exception)
{
string message = "Please try again";
string script = "window.onload = function(){ alert('";
script += message;
script += "')};";
ClientScript.RegisterStartupScript(this.GetType(), "FailedMessage", script, true);
}
finally
{
con.Close();
}
}
Please anyone help me. P/s: I already correct my typing error.
答:
0赞
b166er
2/10/2023
#1
您没有在此处传入 CaseId:
cmd = new SqlCommand("UPDATE Table SET Date=@Date WHERE CaseID=@CaseID", con);
cmd.Parameters.AddWithValue("@Date", stringDate);
评论
0赞
Panagiotis Kanavos
2/10/2023
这只是问题之一。它来自哪里,它包含什么?stringDate
评论
stringDate
stringDate
TargetDate
txtDate
TargetDate