Вопрос

My code gives the above error.

I have tried to change it based on the link below:

String was not recognized as a valid DateTime “ format dd/MM/yyyy”

but it still does not work.

Would be grateful if somebody can point me in the right direction.

My code is below:

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)); Line giving the error

                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();

            }

In design Mode:

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

When debugging dtm value comes as:

{Text = "18/05/2012"}

Это было полезно?

Решение

Try

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

four y instead of 3 and InvariantCulture

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top