locked
can't update a field with date RRS feed

  • Question

  • User-427368358 posted

    Hi

    I' trying to update the date of a field with a new date (replacing the old date by the new). I get no error but it doesnt' work. I guess it has something to do with date format.

    sql server:
    file: begend
    field: begdat as date

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
     Dim begdat As DateTime = DateTime.Parse(e.NewValues("begdat"))
     Dim obegdat As DateTime = DateTime.Parse(e.OldValues("begdat"))

     Using mConnection As New SqlConnection(param.ConnectionString)
     mConnection.Open()
    sql = "update begend set begdat=@begdat where begdat=@obegdat"
    comd = New SqlCommand(sql, mConnection)
    comd.Parameters.AddWithValue("@begdat", Convert.ToDateTime(begdat))
    comd.Parameters.AddWithValue("@obegdat", Convert.ToDateTime(obegdat))
    comd.ExecuteNonQuery()
    End Using
    ---------
    I also tried with
    Dim begdat As Date
    Dim begdat As string
    I tried with and without Convert.ToDateTime

    Thanks

    Raf

    Friday, December 18, 2020 7:50 PM

Answers

  • User-427368358 posted

    I found it by using:

    dim begdat as date and in sql server defining begdat as date.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 22, 2020 3:28 PM

All replies

  • User-427368358 posted

    I forgot to mention that i use an european format. In sql server, the date is like this: 2020-12-18.

    In the aspx file; it's like this:

    <asp:TemplateField HeaderText="begindatum" SortExpression="begdat" ItemStyle-HorizontalAlign="Center" >
    <EditItemTemplate>
    <asp:TextBox ID="begdat" runat="server" ReadOnly="true" Width="60px" Text='<%# Bind("begdat","{0:dd-MM-yy}") %>' ></asp:TextBox>
    <ajaxToolkit:CalendarExtender ID="cal2" runat="server" TargetControlID="begdat" PopupButtonID="calimg" Format="dd-MM-yy"></ajaxToolkit:CalendarExtender>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="begdatl" runat="server" Text='<%# Bind("begdat","{0:dd-MM-yy}") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:TemplateField>

    Friday, December 18, 2020 8:05 PM
  • User475983607 posted

    I recommend basic debugging.  

    Set a break point and single step through your code to find the bug.   Answer the following questions which should help you find the bug. 

    What is the value of begdat and obegdate?

     Dim begdat As DateTime = DateTime.Parse(e.NewValues("begdat"))
     Dim obegdat As DateTime = DateTime.Parse(e.OldValues("begdat"))

    If the variable contains Dates and the dates are correct then there is a different issue.   If the dates are incorrect or throw an exception then you are not adhering to your custom date format.  Use DateTime.TryParse() or DateTime.TryParseExact() and supply the date format you expect. 

    See the following reference doc.

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=net-5.0

    This SQL script does not seem correct.  Usually an ID is used to uniquely identify a record.  Do you expect to update many records? 

    sql = "update begend set begdat=@begdat where begdat=@obegdat"

    Friday, December 18, 2020 8:23 PM
  • User-427368358 posted

    I found it by using:

    dim begdat as date and in sql server defining begdat as date.

    Thx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 22, 2020 3:28 PM