locked
SQLDataSource/webform date fields access RRS feed

  • Question

  • User-1939834628 posted

    Hallo,

    I got a problem trying to manage date fields using SQLDataSource inside a webform.

    I got a very simple page with some text fields and a datefield. I'm using a SQLDataSource control to feed the webform with data.

    Form widgets are bound to sqldatasource data fields trough #Bind() directives. In particular I would like the date field to be bound trough a #Bind("date", "{0:dd/MM/yyyy}") to properly format date for italian users.

    The SelectCommand is defined as a simple "SELECT" statement.

    The UpdateCommand is defined in this way: 

    UPDATE table SET `text_field` = @text_field, `date_field` = @date_field WHERE id=@id;

    I got a number of problems using this form since when I try to update data I can run into a number of different errors:

    1) if I leave the date_field empty I got format error:

    2) if I use a date format different from those used by SQL backend (yyyy-MM-dd) I got format errors;

    I tried to use the OnUpdating event changing the e.Parameters["@date_field"].Value with a properly formatted value but without luck.

    There's a working 'strategy' to manage update of date fields that allows to display and edit data in formats different from standard SQL format?

    Best Regards,

     Weirdgyn

    Sunday, May 17, 2015 6:05 PM

Answers

  • User-1716253493 posted

    try like this

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox datetextbox=(TextBox)GridView1.Rows[e.RowIndex].FindControl("datetextbox");
            if(datetextbox.Text!=string.Empty)
            {
                e.NewValues["date_field"] = DateTime.ParseExact(datetextbox.Text, "dd/MM/yyyy", null);
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 17, 2015 8:17 PM