I’m using a timespan function to work out the number of days, this value is then added to a text box and saved to the database.
I have recently added a radio button as a half day so it if is checked it adds 0.5 days holiday rather than 1 day. This seems to work correctly when displayed in the text box before being saved. However, when it actually saves and updates the database shows a value of 1 rather than 0.5 as shown in text box.
The fieldtype in the database for this field was originally set to ‘int’but when I added the half day it would not save as it was trying to save a decimal figure, I have since changed this fieldtype to ‘decimal’ as it thought this would be more suitable.. i’m not sure if this is correct or it should be something different?
I cannot seem to figure out why it is not saving the value specified in the text box…any suggestions
Private Sub btnHNoDays_Click(sender As Object, e As System.EventArgs) Handles btnHNoDays.Click
'declare dates
Dim dtStart As Date = txtHStart_Date.Text
Dim dtEnd As Date = txtHEnd_Date.Text
'timespan function used to minus one date to another and produce a value
Dim ts As TimeSpan = (dtEnd - dtStart)
If RadioButton1.Checked Then
ts = (dtEnd - dtStart) - TimeSpan.FromDays(0.5)
Else
ts = (dtEnd - dtStart)
End If
' the value is set to to textbox.
txtNoofDays.Text = ts.TotalDays()
' the today days value
Console.WriteLine(ts.TotalDays)
End Sub
Protected Sub btnHRequestSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnHRequestSave.Click
'collect data
Dim Sdate = txtHStart_Date.Text
Dim Edate = txtHEnd_Date.Text
Dim NoofDays = txtNoofDays.Text
Dim notes = TxtHRequestNotes.Text
Dim username = lblHolidayRequestLIU.Text
'define connection
Dim HRequestConnection As New SqlConnection
HRequestConnection.ConnectionString = HolidayRequestSqlDataSource.ConnectionString
'create command
Dim HRequestInsert As New SqlCommand("Insert into HolidayRequests (username, RequestDateStart, RequestDateEnd, RequestTotalDays, RequestNotes) VALUES (@username, @Sdate, @Edate, @NoofDays, @notes)", HRequestConnection)
'define parameters
HRequestInsert.Parameters.Add("username", SqlDbType.NVarChar, 20).Value = lblHolidayRequestLIU.Text
HRequestInsert.Parameters.Add("Sdate", SqlDbType.Date).Value = txtHStart_Date.Text
HRequestInsert.Parameters.Add("Edate", SqlDbType.Date).Value = txtHEnd_Date.Text
HRequestInsert.Parameters.Add("NoofDays", SqlDbType.Decimal).Value = txtNoofDays.Text
HRequestInsert.Parameters.Add("notes", SqlDbType.NVarChar).Value = TxtHRequestNotes.Text
' execute commands
HRequestConnection.Open()
HRequestInsert.ExecuteNonQuery()
lblHolRequestResponse.Text = "Your holidays request has been saved!
End Sub
How have you specified the
decimalcolumn in the database? Have you given it precision and scale (see here)? If not, it defaults todecimal(18,0)which will give you no figures after the decimal point.You should give it a type along the lines of
decimal(5,1)depending on your requirements.Bear in mind that precision is the number of total digits (to the left and right of the decimal point) and scale is the number to the right of the decimal point. Therefore
decimal(5,1)could store up to 9999.9.(I’m assuming you’re using SQL Server)