繁体   English   中英

字符串未被识别为有效的日期时间

[英]String not recognized as valid datetime

我的代码给出了以上错误。

我尝试根据以下链接对其进行更改:

无法将字符串识别为有效的DateTime“格式dd / MM / yyyy”

但它仍然不起作用。

如果有人能指出我正确的方向,将不胜感激。

我的代码如下:

foreach (GridViewRow row in GridView1.Rows)

        {

       // int RowIndex = 0;
       // GridViewRow row = (GridViewRow)GridView1.Rows[RowIndex];

            Int32 intresortID = Convert.ToInt32(Request.QueryString["TypeID"]);
            Label dtm = row.FindControl("Label1") as Label;
            Label strRoomType = row.FindControl("Label2") as Label;
            Label strDescription = row.FindControl("Label3") as Label;
            TextBox Qty = row.FindControl("intQtyTextBox") as TextBox;
            TextBox Price = row.FindControl("curPriceTextBox") as TextBox;
            Label intWSCode = row.FindControl("intWSCodeLabel") as Label;

            string connStr = ConfigurationManager.ConnectionStrings["bestandConnectionString"].ConnectionString;
            using (SqlConnection Con = new SqlConnection(connStr))
            {
                Con.Open();
                SqlCommand cmd = new SqlCommand("Update tblAvail set intqty=@intQty, curprice=@curprice where intresortid=@intresortid and dtm=@dtm and strroomtype=@strroomtype", Con);

cmd.Parameters.AddWithValue(“ @ dtm”,DateTime.ParseExact(dtm.Text.Trim(),“ dd / MM / yyyy”,null)); 行给出错误

                cmd.Parameters.AddWithValue("@strroomtype", strRoomType.Text.Trim());
                cmd.Parameters.AddWithValue("@intQty", Qty.Text.Trim());
                cmd.Parameters.AddWithValue("@curPrice", Price.Text.Trim());
                cmd.Parameters.AddWithValue("@intResortID", intresortID);

                cmd.ExecuteNonQuery();
                GridView1.EditIndex = -1;
                DataBind();

            }

在设计模式下:

  <ItemTemplate>
        <asp:Label ID="Label1" runat="server" 
                        Text='<%# Eval("Dtm", "{0:dd/MM/yyyy}") %>'></asp:Label>
  </ItemTemplate>

调试dtm值时如下:

{Text =“ 18/05/2012”}

尝试

DateTime.ParseExact(dtm.Text.Trim(), "dd/M/yyyy", System.Globalization.CultureInfo.InvariantCulture)

四个y而不是3和InvariantCulture

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM