【发布时间】:2016-04-12 03:38:46
【问题描述】:
每次我从数据库保存或更新我的数据时,我都会遇到错误,提示从字符串到十进制类型的转换无效。 不知道哪里出了问题
这是我的代码:
Private Sub txtmRate_TextChanged(sender As Object, e As EventArgs) Handles txtmRate.TextChanged
txtmRate.Text = Format(CDbl(txtmRate.Text), "#,##0.00")
End Sub 'For Textbox Change
Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
If txtPos.Text = "" Or txtDept.Text = "" Or txtmRate.Text = "" Then
MessageBox.Show("Please fill up all the details!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
If MessageBox.Show("Do you really want to add this new data?", "Save!", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = vbYes Then
sqlqry = "INSERT INTO tblPosition(WorkPosition,Department,MonthlyRate) VALUES (@wPos,@Dept,@mRate)"
Try
ConnDB()
cmd = New OleDbCommand(sqlqry, cnn)
cmd = cnn.CreateCommand
With cmd
.Connection = cnn
.CommandText = sqlqry
.CommandType = CommandType.Text
.Parameters.AddWithValue("@wPos", OleDbType.VarWChar = 50).Value = txtPos.Text
.Parameters.AddWithValue("@Dept", OleDbType.VarWChar = 50).Value = txtDept.Text
.Parameters.AddWithValue("@mRate", OleDbType.Currency = 10).Value = txtmRate.Text
.ExecuteNonQuery()
End With
MessageBox.Show("New Data successfully added!", "Save!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
RefreshData()
cmd.Dispose()
ctrl_clear()
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 Saving Record
更新记录代码
Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) 处理 cmdUpdate.Click
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
【问题讨论】:
标签: vb.net ms-access-2010