我有一个文本框,我想在其中输入十进制数字。问题是没有保存实际数字。而是将输入的数字向上舍入。因此,如果我输入1.5,则输入2.0。
带有值的文本框
Dim _case As Decimal = Decimal.Parse(txtCase.Text)
我在SO上发现了其他四舍五入问题,但似乎没有一个类似于我的问题。 我还检查了https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/decimal-data-type上的文档,但似乎没有关于此问题的任何想法。
在SQL Server中应将值保存到的列
这可能很简单,但我似乎看不到。
有什么想法吗?
编辑。也尝试过:
Dim _case As String = Convert.ToDecimal(txtCase.Text)
完整代码
Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAddUpdate.Click
'Constants
Const _addAdjustmentText As String = "Add Adjustment"
Const _updateText As String = "Update"
'Declare the fields
Dim _refYear As Integer = Integer.Parse(txtRefYear.Text)
Dim _refMonth As String = ddlMonth.SelectedIndex.ToString()
Dim _branchCode As String = txtBranchCode.Text
Dim _employeeCode As String = txtEmployeeCode.Text
Dim _policyNumber As Long = Long.Parse(txtPolicyNumber.Text)
Dim _extraPolicyType As String = ddlExtraPolicyType.SelectedIndex.ToString()
Dim _premium As Decimal = Decimal.Parse(txtPremium.Text)
Dim _case As Decimal = Decimal.Parse(txtCase.Text)
Dim _planCode As String = txtPlanCode.Text
Dim _planDescription As String = txtPlanDescription.Text
If btnAddUpdate.Text = _addAdjustmentText Then
Try
Company.Applications.ProductionEngine.BusinessAccess.ManualAdjustmentsBusinessAccess.InsertNewAdjustment(_refYear,
_refMonth,
_branchCode,
_employeeCode,
_policyNumber,
_extraPolicyType,
_premium,
_case,
_planCode,
_planDescription)
InformationBox1.ShowSuccessMessage("Adjustment inserted")
clearAllFields()
loadManualAdjustments()
Catch ex As Exception
InformationBox1.ShowErrorMessage("An internal error occured. Please check values and try again.")
End Try
ElseIf btnAddUpdate.Text = _updateText Then
'Update field based on RowId. txtUpdateRowId Hidden field.
Dim _rowId As Integer = Convert.ToInt32(txtUpdateRowId.Text.ToString())
Try
Company.Applications.ProductionEngine.BusinessAccess.ManualAdjustmentsBusinessAccess.UpdateManualAdjustment(_refYear,
_refMonth,
_branchCode,
_employeeCode,
_policyNumber,
_extraPolicyType,
_premium,
_case,
_planCode,
_planDescription,
_rowId)
InformationBox1.ShowSuccessMessage("Adjustment updated")
btnAddUpdate.Text = "Add Adjustment"
clearAllFields()
loadManualAdjustments()
Catch ex As Exception
InformationBox1.ShowErrorMessage("An internal error occured. Please check values and try again.")
End Try
End If
End Sub
业务层
Public Shared Function UpdateManualAdjustment(ByVal _refYear As Integer,
ByVal _refMonth As String,
ByVal _branchCode As String,
ByVal _employeeCode As String,
ByVal _policyNumber As Integer,
ByVal _extraPolicyType As String,
ByVal _premium As Integer,
ByVal _case As Decimal,
ByVal _planCode As String,
ByVal _planDescription As String,
ByVal _rowId As Integer)
数据访问层
Public Shared Function UpdateManualAdjustment(ByVal _refYear As Integer,
ByVal _refMonth As String,
ByVal _branchCode As String,
ByVal _employeeCode As String,
ByVal _policyNumber As Integer,
ByVal _extraPolicyType As String,
ByVal _premium As Integer,
ByVal _case As Decimal,
ByVal _planCode As String,
ByVal _planDescription As String,
ByVal _rowId As Integer)
Return Company.Applications.Data.SqlHelper.ExecuteNonQuery(Company.Applications.Data.ConnectionStrings.getProductionEngine, _
CommandType.StoredProcedure, _
"BR_NAME_MANUAL_ADJUSTMENTS_UPDATE", _
New SqlClient.SqlParameter("year", _refYear),
New SqlClient.SqlParameter("month", _refMonth),
New SqlClient.SqlParameter("branchCode", _branchCode),
New SqlClient.SqlParameter("employeeCode", _employeeCode),
New SqlClient.SqlParameter("policyNumber", _policyNumber),
New SqlClient.SqlParameter("extraPolicyType", _extraPolicyType),
New SqlClient.SqlParameter("premium", _premium),
New SqlClient.SqlParameter("case", _case),
New SqlClient.SqlParameter("planCode", _planCode),
New SqlClient.SqlParameter("planDescription", _planDescription),
New SqlClient.SqlParameter("rowId", _rowId))
End Function
.ASPX文件
<asp:TableCell>
<asp:TextBox ID="txtCase" runat="server" Width="90px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCase" runat="server" ControlToValidate="txtCase"
ErrorMessage="Required" ForeColor="Red" Display="Dynamic" ValidationGroup="Insert"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revCase" runat="server" ErrorMessage="Decimal Field"
ControlToValidate="txtCase" ForeColor="Red" ValidationExpression="^\d+([,\.]\d{1,2})?$"></asp:RegularExpressionValidator>
</asp:TableCell>
存储过程
ALTER PROCEDURE [dbo].[MY_STORED_PROCEDURE]
@year INT,
@month VARCHAR(50),
@branchCode VARCHAR(50),
@employeeCode VARCHAR (50),
@policyNumber BIGINT,
@extraPolicyType VARCHAR(50),
@premium DECIMAL,
@case DECIMAL,
@rowId INT,
@planCode VARCHAR(5),
@planDescription VARCHAR(50)
AS
BEGIN
UPDATE BR_NAME_MANUAL_ADJUSTMENTS
SET RefYear = @year,
RefMonth = @month,
BranchCode = @branchCode,
EmployeeCode = @employeeCode,
PolicyNumber = @policyNumber,
ExtraPolicyType = @extraPolicyType,
Premium = @premium,
Cases = @case,
PlanCode = @planCode,
PolicyPlanDescription = @planDescription
WHERE RowId = @rowId
END
答案 0 :(得分:0)
检查存储过程,并确保“ case”参数的数据类型为小数(18,2)