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
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.
Member
3 Points
48 Posts
can't update a field with date
Dec 18, 2020 07:50 PM|raffarin|LINK
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
Member
3 Points
48 Posts
Re: can't update a field with date
Dec 18, 2020 08:05 PM|raffarin|LINK
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>
All-Star
52241 Points
23304 Posts
Re: can't update a field with date
Dec 18, 2020 08:23 PM|mgebhard|LINK
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?
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?
Member
3 Points
48 Posts
Re: can't update a field with date
Dec 22, 2020 03:28 PM|raffarin|LINK
I found it by using:
dim begdat as date and in sql server defining begdat as date.
Thx