I have a web page in which there is a textbox that asks my users to enter a start date. What I'm trying to do from there is that no matter what the end date is, my page will calculated that from the text box, store it as a variable and pass it to my stored procedure. Here is the page I have so far:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles wasoncallButton.Click
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim newDate As DateTime
        Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"
        Dim con As New SqlConnection(connectionString)
        con.Open()
        cmd.Connection = con
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "sp_owncalls3"
        cmd.Parameters.AddWithValue("@start", dateTextBox.Text)


        Try
            If newDate.Date.AddDays(1).AddSeconds(-1).ToString() Then
                cmd.Parameters.AddWithValue("@dayend", newDate)
            Else
                errorlabel.Text = "Invalid Date"
            End If
        Catch ex As Exception
            Response.Write("Error:" & ex.ToString)
        End Try
    End Sub


End Class

The two variables here are @start and @dayend. @start will be gathered from the text box, but I need to know how to calculate and save the variable for @dayend. Any help would be appreciated.

Thank you,

Doug

Dani AI

Generated

This thread describes a single-textbox workflow: parse a user-entered start date, compute that day's end timestamp, and pass both values to a stored procedure. asked whether dayend is a date/time (it is), and pointed out the basic calculation. Recommended changes below address input validation, fractional-second precision, and parameter typing so the stored-proc call is reliable and culture-safe.

Example (VB.NET): validate the textbox, compute true end-of-day, and add typed parameters rather than sending strings.

Dim input As String = dateTextBox.Text.Trim()
Dim startDate As DateTime

If DateTime.TryParseExact(input, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, startDate) Then
    Dim dayEnd As DateTime = startDate.Date.AddDays(1).AddTicks(-1)   ' last tick of the day
    cmd.Parameters.Add("@start", SqlDbType.DateTime2).Value = startDate
    cmd.Parameters.Add("@dayend", SqlDbType.DateTime2).Value = dayEnd
    cmd.ExecuteNonQuery()
Else
    errorLabel.Text = "Invalid date format"
End If

Notes and cautions:

  • Use TryParseExact (or TryParse) to avoid culture/format ambiguity (DateTime.TryParseExact).
  • Subtracting one tick from midnight (AddTicks(-1)) yields the precise last instant of the day; it’s preferred over subtracting whole seconds (DateTime.AddTicks).
  • Pass DateTime objects to SQL parameters and specify SqlDbType (e.g., DateTime2) instead of relying on AddWithValue to avoid type/precision surprises (SqlParameterCollection.AddWithValue; datetime2).
  • If time zones matter, consider DateTimeOffset/datetimeoffset instead (DateTimeOffset).
  • Check the stored procedure’s expected semantics: some code prefers an exclusive upper bound (>= start AND < nextMidnight) instead of an inclusive end timestamp.

These changes address parsing, precision, and SQL typing so the @start / @dayend values are safe and precise when calling your stored procedure.

Recommended Answers

All 4 Replies

I don't get it. Do you have one textbox for @start and one for @dayend?
@Dayend is a number or a date?

I don't get it. Do you have one textbox for @start and one for @dayend?
@Dayend is a number or a date?

I don't have a textbox for @dayend, what I want to do is calculate the day end based on the values that the user types in the textbox for @start. @Dayend is a date/time.

And how do you calculate the @dayend?

You're already calculating @dayend

newDate.Date.AddDays(1).AddSeconds(-1).ToString()

I don't understand why you have an "If" in the front of it though?!

The way I would do it would be:

Try
cmd.Parameters.AddWithValue("@dayend", newDate.Date.AddDays(1).AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss.fff"))
cmd.ExecuteNonQuery()
Catch ex As Exception
errorlabel.Text = "Invalid Date"
End Try

I hope that helps...


- Looking for a new job?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.