問題描述
為什么我在運行此代碼時會出現將 varchar 轉換為浮點數的錯誤?
Why do I get an error converting varchar into float conversion when I run this code?
我不明白,請幫忙.
Imports System.Data.SqlClient
Public Class Form1
Dim selected As Double
Dim HourItem As Double
Dim OverTimeRate As Double
Dim connection As New SqlConnection("Data Source=DESKTOP-F55AVQ6\SQLEXPRESS;Initial Catalog=Db_Suncrest_Financial;Integrated Security=True")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Db_Suncrest_FinancialDataSet.Tb_Suncrest_Financial_Payroll' table. You can move, or remove it, as needed.
Me.Tb_Suncrest_Financial_PayrollTableAdapter.Fill(Me.Db_Suncrest_FinancialDataSet.Tb_Suncrest_Financial_Payroll)
End Sub
Private Sub Employee_PositionComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Employee_PositionComboBox.SelectedIndexChanged
Select Case Employee_PositionComboBox.SelectedItem
Case "Banker"
selected = 14.75
Case "Bank Teller"
selected = 10
Case "Loan Processor"
selected = 17.1
Case "Mortgage Consultant"
selected = 19.22
Case "Investment Representative"
selected = 19.31
Case "Credit Analyst"
selected = 19.88
Case "Investment Banker"
selected = 22.75
Case "Relationship Manager"
selected = 23.85
Case "Financial Adviser"
selected = 23.99
Case "Financial Analyst"
selected = 25.84
Case "Asset Manager"
selected = 26.86
Case "Under Writer"
selected = 27.56
Case "Internal Auditor"
selected = 28.79
Case "Investment Banking Analyst"
selected = 39.37
Case "Loan Officer"
selected = 43.18
End Select
Rate_Per_HourTextBox.Text = selected
End Sub
Private Sub Hours_WorkedComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Hours_WorkedComboBox.SelectedIndexChanged
Select Case Hours_WorkedComboBox.SelectedItem
Case "8"
HourItem = 8
Case "7"
HourItem = 7
Case "6"
HourItem = 6
Case "5"
HourItem = 5
Case "4"
HourItem = 4
End Select
End Sub
Private Sub OverTime_RateComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles OverTime_RateComboBox.SelectedIndexChanged
Select Case OverTime_RateComboBox.SelectedItem
Case "Normal Day"
OverTimeRate = 1.25
Case "Rest Day"
OverTimeRate = 1.69
Case "Special Non-Working Hoiday"
OverTimeRate = 1.69
Case "Special Non-Working Hoiday + Rest Day"
OverTimeRate = 1.95
Case "Regular Holliday"
OverTimeRate = 2.6
Case "Regular Holliday + Rest Day"
OverTimeRate = 3.38
End Select
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Basic_PayTextBox.Text = HourItem * selected * 30
OverTime_PayTextBox.Text = OverTimeRate * selected * OverTime_HoursTextBox.Text
Gross_SalaryTextBox.Text = OverTime_PayTextBox.Text + Basic_PayTextBox.Text
Total_Salary_W_TaxTextBox.Text = Gross_SalaryTextBox.Text * (0.0034 + 0.0363 + 0.01375)
Date_TimeTextBox.Text = Format(Now, "General Date")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
connection.Open()
Dim command As New SqlCommand("Insert into Tb_Suncrest_Financial_Payroll(Employee_Id_No, Employee_Name, Employee_Position, Residence, Date_Time, Hours_Worked, Rate_Per_Hour,
Basic_Pay, OverTime_Hours, OverTime_Rate, OverTime_Pay, Gross_Salary, SSS, Pagibig, Philhealth, Total_Salary_W_Tax)
Values(
'" & Employee_Id_NoTextBox.Text & "', '" & Employee_NameTextBox.Text & "', '" & Employee_PositionComboBox.Text & "', '" & ResidenceTextBox.Text & "', '" & Date_TimeTextBox.Text & "',
'" & Hours_WorkedComboBox.Text & "', '" & Rate_Per_HourTextBox.Text & "', '" & Basic_PayTextBox.Text & "', '" & OverTime_HoursTextBox.Text & "', '" & OverTime_RateComboBox.Text & "',
'" & OverTime_PayTextBox.Text & "', '" & Gross_SalaryTextBox.Text & "', '" & SSSTextBox.Text & "', '" & PagibigTextBox.Text & "', '" & PhilhealthTextBox.Text & "', '" & Total_Salary_W_TaxTextBox.Text & "')", connection)
command.Parameters.AddWithValue("@Employee_Id_No", SqlDbType.Int)
command.Parameters.AddWithValue("@Employee_Name", SqlDbType.VarChar)
command.Parameters.AddWithValue("@Employee_Position", SqlDbType.VarChar)
command.Parameters.AddWithValue("@Residence", SqlDbType.VarChar)
command.Parameters.AddWithValue("@Date_Time", SqlDbType.DateTime)
command.Parameters.AddWithValue("@Hours_Worked", SqlDbType.Int)
command.Parameters.AddWithValue("@Rate_Per_Hour", SqlDbType.Float)
command.Parameters.AddWithValue("@Basic_Pay", SqlDbType.Float)
command.Parameters.AddWithValue("@OverTime_Hours", SqlDbType.Float)
command.Parameters.AddWithValue("@OverTime_Rate", SqlDbType.Float)
command.Parameters.AddWithValue("@OverTime_Pay", SqlDbType.Float)
command.Parameters.AddWithValue("@Gross_Salary", SqlDbType.Float)
command.Parameters.AddWithValue("@SSS", SqlDbType.Float)
command.Parameters.AddWithValue("@Pagibig", SqlDbType.Float)
command.Parameters.AddWithValue("@Philhealth", SqlDbType.Float)
command.Parameters.AddWithValue("@Total_Salary_W_Tax", SqlDbType.Float)
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("Added")
Else
MessageBox.Show("Not Added")
End If
connection.Close()
End Sub
End Class
推薦答案
以下是@Charlieface 出色回答提出的想法的示例.
Here is an example of the ideas presented by @Charlieface excellent answer.
另外幾點.
ID 字段是身份字段是很常見的(自動編號) 檢查您的數據庫.如果是這種情況,那么您將不會在字段列表中包含 Employee_ID
,值列表將任何內容添加到該字段的參數集合中.在您的情況下,這似乎是一個工資表,因此 Employee_ID
可能是其他地方員工表的外鍵.在這種情況下,代碼沒問題.
It is very common for ID fields to be identity fields (auto-number) Check your database.
If this is the case then you would not include Employee_ID
in the fields list, Values list of add anything to the parameters collection for that field. In your case this appears to be a payroll table so perhaps Employee_ID
is a foreign key to an employees table elsewhere. In this case, the code is fine.
用戶很容易輸入任何TextBox
.我強烈建議您在嘗試插入之前使用 .TryParse
驗證數字和日期的所有輸入.
Users are apt to enter anything is a TextBox
. I strongly suggest that you validate all the input for numbers and dates with .TryParse
before attempting the insert.
不要在連接打開時顯示消息框.連接是寶貴的資源,應該在 .Execute...
之前直接打開,并通過 End Using
盡快關閉.
Never present a message box while a connection is open. Connections are precious resources and should be opened directly before the .Execute...
and closed as soon as possible with End Using
.
似乎有幾個計算字段根本不需要存儲,但存儲很便宜,而且可能會使其他查詢更簡單.
There appears to be several calculated fields that do not need to be stored at all but storage is cheap and it will probably make other queries simpler.
Private ConStr As String = "Your connection string"
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim RetVal As Integer
Dim Sql = "Insert into Tb_Suncrest_Financial_Payroll(Employee_Id_No, Employee_Name, Employee_Position, Residence, Date_Time, Hours_Worked, Rate_Per_Hour,
Basic_Pay, OverTime_Hours, OverTime_Rate, OverTime_Pay, Gross_Salary, SSS,
Pagibig, Philhealth, Total_Salary_W_Tax)
Values(
@Employee_Id_No, @Employee_Name, @Employee_Position, @Residence, @Hours_Worked,@Rate_Per_Hour,
@Basic_PayTextBox, @OverTime_Hours, @OverTime_Rate, @Gross_Salary, @SSS,
@Pagibig, @Philhealth, @Total_Salary_W_Tax);"
Using connection As New SqlConnection(ConStr),
command As New SqlCommand(Sql, connection)
With command.Parameters
.Add("@Employee_Id_No", SqlDbType.Int).Value = CInt(Employee_Id_NoTextBox.Text)
.Add("@Employee_Name", SqlDbType.VarChar).Value = Employee_NameTextBox.Text
.Add("@Employee_Position", SqlDbType.VarChar).Value = Employee_Position.Text
.Add("@Residence", SqlDbType.VarChar).Value = ResidenceTextBox.Text
.Add("@Date_Time", SqlDbType.DateTime).Value = CDate(Date_TimeTextBox.Text)
.Add("@Hours_Worked", SqlDbType.Int).Value = CInt(Hours_WorkedComboBox.Text)
.Add("@Rate_Per_Hour", SqlDbType.Float).Value = CDbl(Rate_Per_HourTextBox.Text)
.Add("@Basic_Pay", SqlDbType.Float).Value = CDbl(Basic_PayTextBox.Text)
.Add("@OverTime_Hours", SqlDbType.Float).Value = CDbl(OverTime_HoursTextBox.Text)
.Add("@OverTime_Rate", SqlDbType.Float).Value = CDbl(OverTime_RateComboBox.Text)
.Add("@OverTime_Pay", SqlDbType.Float).Value = CDbl(OverTime_PayTextBox.Text)
.Add("@Gross_Salary", SqlDbType.Float).Value = CDbl(Gross_SalaryTextBox.Text)
.Add("@SSS", SqlDbType.Float).Value = CDbl(SSSTextBox.Text)
.Add("@Pagibig", SqlDbType.Float).Value = CDbl(PagibigTextBox.Text)
.Add("@Philhealth", SqlDbType.Float).Value = CDbl(PhilhealthTextBox.Text)
.Add("@Total_Salary_W_Tax", SqlDbType.Float).Value = CDbl(Total_Salary_W_TaxTextBox.Text)
End With
connection.Open()
RetVal = command.ExecuteNonQuery
End Using
If RetVal = 1 Then
MessageBox.Show("Added")
Else
MessageBox.Show("Not Added")
End If
End Sub
這篇關于為什么會出現“數據類型轉換錯誤"?使用 ExecuteNonQuery()?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!