从字符串“”到“Deicmal”类型的转换无效

时间:2016-04-12 03:38:46

标签: vb.net ms-access-2010

我有一个错误,说每次从数据库保存或更新数据时,从字符串转换为十进制类型都无效。 我不知道问题出在哪里

这是我的代码:

    If txtPos.Text = "" Or txtDept.Text = "" Or txtmRate.Text = "" Then
        MessageBox.Show("Please fill the required fields!", "Update!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        If MessageBox.Show("Do you really want to Update Information! Continue?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim sqlqry As String = "UPDATE tblPosition SET [WorkPosition]=@wPos, [Department]=@Dept, [MonthlyRate]=@mRate WHERE [CounterID]=@cID"
            Try
                ConnDB()
                cmd = cnn.CreateCommand
                With cmd
                    .Connection = cnn
                    .CommandText = sqlqry
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@wPos", OleDbType.VarWChar = 100).Value = txtPos.Text
                    .Parameters.AddWithValue("@Dept", OleDbType.VarWChar = 100).Value = txtDept.Text
                    .Parameters.AddWithValue("@mRate", OleDbType.Decimal = 10).Value = txtmRate.Text
                    .Parameters.AddWithValue("@cID", OleDbType.Guid = 5).Value = txtCountID.Text
                    .ExecuteNonQuery()
                End With
                MessageBox.Show("Position Information has been updated!", "Update!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                cmd.Dispose()
                RefreshData()
                ctrl_clear()
                btn_eAddSaveCancel()
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
            End Try
        End If
    End If
End Sub  ' For Updating existing Data

更新记录的代码

Private Sub cmdUpdate_Click(sender As Object,e As EventArgs)处理cmdUpdate.Click

index ActionResult

3 个答案:

答案 0 :(得分:0)

尝试将您的txtmRate文本框valur转换为十进制,然后在dB更新语句中绑定它的值。这样的事情应该有效

Parameters.AddWithValue(“@ mRate”,OleDbType.Decimal = 10).Value = Convert.ToDecimal(txtmRate.Text)

答案 1 :(得分:0)

我将textchange事件更改为:

    Dim aNumber As Double
    If Double.TryParse(txtmRate.Text, aNumber) Then
        txtmRate.Text = String.Format("{0:n2}", aNumber)
    End If

它解决了我的问题,感谢BTW的帮助。

答案 2 :(得分:-1)

这里你只需改变这一行..

.Parameters.AddWithValue("@mRate", OleDbType.Currency = 10).Value = Convert.ToDecimal(txtmRate.Text)

更新代码......

.Parameters.AddWithValue("@mRate", OleDbType.Decimal = 10).Value = Convert.ToDecimal(txtmRate.Text)

在这两种情况下,您都会传递类型为decimal的参数,textbox会返回varchar。所以你必须将varchar转换为十进制。

它绝对适合你......