I'm working on a vb.net application (I'm very new to vb.net), and have just added a DatePicker
to a field on a form- PostDate
(the field was previously just a text box).
The date picker appears to work visually- when the user clicks inside the form field, the date picker calendar appears, and they can select a date from the current month, or switch months to select a date from another month. Once the user has selected a date, the calendar disappears, and the newly selected date is displayed in the field in the format mm/dd/yyyy.
However, while debugging my application, when I reach the UpdateButton_Click()
function that is handling the updates to fields in the form (i.e. checking to see if any of their values have changed, and updating them in the database if they have), I see that in the line I have added to check for changes to the date field that I have just added the DatePicker to, the date variable still holds the previous value (i.e. the original date, rather than the newly selected date).
The UpdateButton_Click()
function is defined with:
Protected Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
If Page.IsPostBack Then
...
Dim mypostdate As String = HttpUtility.HtmlEncode(PostDate.Text & " " & theTime.Text)
...
C.updateCaseByCaseID(... mypostdate, ...)
...
Next
End If
End Sub
If I put a break point on this function, and step through it, I can see that the value of PostDate.Text
is always its original value (i.e. it has a previously saved value when the page loads). Even if I change the value of the field to a new date, using the date picker, although the field shows the new date in the browser, when I step through this function, the value of PostDate.Text
is always the original date value.
The call to C.updateCaseByCaseID(...)
appears to be what is handling the database updates, and was originally defined in cases.vb with:
Public Function updateCaseByCaseID(ByVal Caseid As String, ...) As String
Dim con As New SqlConnection(connectionString)
Dim sql As String = "UPDATE tableName SET "
sql = sql & " ReportNumber=@ReportNumber, ... "
If status = "" Then
Else
sql = sql & " ,status=status "
End If
...
If PostDate = "" Then
Else
sql = sql & ", PostDate=@PostDate"
End If
' Other similar If Else statements for the other parameters '
...
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@CID", Cid)
...
cmd.Parameters.AddWithValue("@PostDate", PostDate)
' Other similar AddWithValue statements '
...
If state = "" Then
Else
cmd.Parameters.AddWithValue("@State", State)
End If
' Other similar If statements for the other parameters '
...
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Dim myaction As String = "Done"
Return myaction
End Function
I added the postdate
parameter to the sql
variable by changing that line to:
sql = sql & " ReportNumber=@ReportNumber, ..., PostDate=@PostDate, ...
However, although this does now update the value of the post date field, it appears to update it to today's date, rather than the date selected by the DatePicker. This seems to be because although the PostDate
field displays the newly selected date in the browser, in the UpdateButton_Click(...)
function, in the line
Dim mypostDate As String = HttpUtility.HtmlEncode(PostDate.Text + " " + theTime.Text)
the variable PostDate.Text
, i.e. the form field that is displaying the newly selected date in the browser, appears to have the value of today's date... I don't understand why this is happening...? Surely it should contain the value of the date selected in the DatePicker? Especially given that that is what's being displayed in the browser...
Anyone have any suggestions what I'm doing wrong with this?
Edit
As mentioned in the comments, I have updated the UpdateButton_Click()
function as follows:
Protected Sub UpdateButton_Click(sender As Object, e As EventArgs) Hanles UpdateButton.Click
If Page.IsPostBack Then
...
Dim mypostdate As String = HttpUtility.HtmlEncode(PostDateTxt.Text & " " & theTime.Text)
Dim postDateAndTimeParts = mypostdate.Split("/")
Dim postTime = postDateAndTimeParts(2).Split("/")
Dim postDay = postDateAndTimeParts(1)
Dim postMonth = postDateAndTimeParts(0)
Dim postYear = postDateAndTimeParts(2).Split(" ")(0)
Dim postHour = postTime.Split(":")(0)
Dim postMin = PostTime.Split(":")(1)
DateTime PostDate = New DateTime(postYear, postMonth, postDay, postHour, postMin, 0);
...
C.updateCaseByCaseID(...)
End If
End Sub
My intention is to pass the new PostDate
DateTime object that I'm reconstructing from the String into the C.updateCaseByCaseId(...)
call, however on the line where I'm constructing that object, I'm currently getting a compile error that says:
Method arguments must be enclosed in parentheses
I'm not sure why this is? I can't see where I'm missing any parentheses...