Reputation: 121
I tried to insert a STRING
from a TextBox
to the COLUMN
that has a MONEY DATA TYPE
but an error prompt appeared and says : Error Occured, Can't Add Patient! Disallowed implicit conversion from data type nvarchar to data type money, table 'TblName', column 'pTotalDue'. Use the CONVERT function to run this query
how can I CONVERT it? could someone tell me how to do it?
here is my codes :
Private Function INSERT() As String
Dim SQLcon As New SqlConnection
Dim SQLdr As SqlDataReader
Try
SQLcon.ConnectionString = "Data Source=#####;Initial Catalog=#####;Persist Security Info=True;User ID=#####;Password=#####"
Dim SQLcmd As New SqlCommand("INSERT INTO dbo.Patients" & _
"(pIDNo,pLName,pFName,pMI,pSex,pStatus,pTelNo,pDocID,pAddr,pStreet,pBarangay,pCity,pProvince,pLNameKIN,pFNameKIN,pMIKIN,pRelationKIN,pTotalDue)" & _
"VALUES(@pIDNo,@pLName,@pFName,@pMI,@pSex,@pStatus,@pTelNo,@pDocID,@pAddr,@pStreet,@pBarangay,@pCity,@pProvince,@pLNameKIN,@pFNameKIN,@pMIKIN,@pRelationKIN,@totaldue)", SQLcon)
SQLcmd.Parameters.AddWithValue("@pIDNo", txtPNumber.Text)
SQLcmd.Parameters.AddWithValue("@pLName", txtLname.Text)
SQLcmd.Parameters.AddWithValue("@pFName", txtFname.Text)
SQLcmd.Parameters.AddWithValue("@pMI", txtMI.Text)
SQLcmd.Parameters.AddWithValue("@pSex", txtPatientSex.Text)
SQLcmd.Parameters.AddWithValue("@pStatus", txtStatus.Text)
SQLcmd.Parameters.AddWithValue("@pTelNo", txtPatientTelNo.Text)
SQLcmd.Parameters.AddWithValue("@pDocID", txtPatientDoctor.Text)
SQLcmd.Parameters.AddWithValue("@pAddr", txtStreetNumber.Text)
SQLcmd.Parameters.AddWithValue("@pStreet", txtStreetName.Text)
SQLcmd.Parameters.AddWithValue("@pBarangay", txtBarangay.Text)
SQLcmd.Parameters.AddWithValue("@pCity", txtCity.Text)
SQLcmd.Parameters.AddWithValue("@pProvince", txtProvince.Text)
SQLcmd.Parameters.AddWithValue("@pLnameKIN", txtKinLname.Text)
SQLcmd.Parameters.AddWithValue("@pFnameKIN", txtKinFname.Text)
SQLcmd.Parameters.AddWithValue("@pMIKIN", txtKinMI.Text)
SQLcmd.Parameters.AddWithValue("@pRelationKIN", txtRelationToPatient.Text)
SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)
SQLcon.Open()
MsgBox("Patient Added!", MsgBoxStyle.Information)
SQLdr = SQLcmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show("Error Occured, Can't Add Patient!" & ex.Message)
Finally
SQLcon.Close()
End Try
Return ""
End Function
BTW the version of SQL SERVER that I'm currently using is 2005.
Upvotes: 1
Views: 5401
Reputation: 9546
Try changing this line:
SQLcmd.Parameters.AddWithValue("@totaldue", txtTotalDue.Text)
to this:
SQLcmd.Parameters.Add("@totaldue", SqlDbType.Money).Value = Decimal.Parse(txtTotalDue.Text)
Upvotes: 3