“从字符串转换为 Double 类型无效”错误 VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21607817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Conversion from string to type Double is not valid" error VB.NET
提问by user3275784
I'm trying to display a logged in users balance from a database in VB. However once I click the Check Balance button it produces the error Conversion from string "Your balance is " to type 'Double' is not valid.
我正在尝试从 VB 中的数据库显示登录用户余额。但是,一旦我单击 Check Balance 按钮,它就会产生错误Conversion from string "Your balance is " to type 'Double' is not valid。
I've tried different ways a converting it from a string to a double, I thought maybe it was because I had m_decBalance declared as a decimal, but that didn't change anything. Can anyone help me? Here's my code:
我尝试了不同的方法将它从字符串转换为双精度值,我想可能是因为我将 m_decBalance 声明为小数,但这并没有改变任何东西。谁能帮我?这是我的代码:
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class Form1
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLcmd As MySqlCommand
Dim DataReader As MySqlDataReader
Private m_strPass As String
Private m_decBalance As Decimal
Private m_strName As String
Private m_strUserPass As String
Private m_strCardNumber As String
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
'Assign users guessed password to variable
m_strUserPass = txtPass.Text
'Invoke
RetrieveAccountInformation()
' determine if Password is correct or not
If m_strUserPass = m_strPass Then
lblWelcome.Visible = True
lblWelcome.Text = "Welcome" + " " + m_strName
txtPass.Enabled = False
btnBalance.Enabled = True
Else
' indicate that incorrect password was provided
lblWelcome.Visible = True
lblWelcome.Text = "Sorry, Password is incorrect." _
& "Please retry ."
' clear user's previous PIN entry
m_strUserPass = ""
End If
txtPass.Clear() ' clear TextBox
End Sub
' load application Form
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Prepare connection and query
Try
dbCon = New MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=mysql")
strQuery = "SELECT CardNumber " &
"FROM Account"
SQLcmd = New MySqlCommand(strQuery, dbCon)
'Open the connection
dbCon.Open()
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
' fill ComboBox with account numbers
While DataReader.Read()
cboAccountNumbers.Items.Add(DataReader("CardNumber"))
End While
'Close the connection
DataReader.Close()
dbCon.Close()
Catch ex As Exception
'Output error message to user with explaination of error
MsgBox("Failure to communicate" & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
' invoke when user provides account number
Private Sub RetrieveAccountInformation()
' specify account number of record from which data will be retrieved
dbCon = New MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=mysql")
strQuery = "SELECT Name, Balance, Password " &
"FROM Account WHERE CardNumber='" & Val(cboAccountNumbers.Text) & "' "
SQLcmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open() ' open database connection
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
DataReader.Read() ' open data reader connection
' retrieve Password number, balance amount and name information from database
m_strPass = Convert.ToString(DataReader("Password"))
m_decBalance = Convert.ToString(DataReader("Balance"))
m_strName = Convert.ToString(DataReader("Name"))
DataReader.Close() ' close data reader connection
dbCon.Close() ' close database connection
End Sub ' RetrieveAccountInformation
Private Sub btnBalance_Click(sender As Object, e As EventArgs) Handles btnBalance.Click
'Retrieve their account information
RetrieveAccountInformation()
Try
MsgBox("You balance is " + " " + m_decBalance)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
回答by Grahamvs
The problem is that you are joining a string with a +. You need to replace the +with &. I also recommend adding .ToStringto your m_decBalance, as this will tell the compiler to treat m_decBalanceas a string, like so:
问题是你加入了一个带有+. 您需要更换+用&。我还建议添加.ToString到您的m_decBalance,因为这会告诉编译器将其m_decBalance视为 a string,如下所示:
MsgBox("You balance is " & " " & m_decBalance.ToString)
The reason you are getting the error is that the compiler tries to convert the string to a numeric value when +is used with a number. For example, the following will display a messagebox with the value of 10:
您收到错误的原因是编译器在与数字一起+使用时尝试将字符串转换为数值。例如,以下将显示一个值为 的消息框10:
MsgBox("5 " + " " + 5)
and
和
Dim Val As Integer = "20 " + 15
will result in Valbeing 35
会导致Val被35
When you want to join strings, I recommend using the &, as this tells the compiler that you don't wish to convert the string to a number, and instead you wish to join them as strings.
当您想连接字符串时,我建议使用&,因为这告诉编译器您不希望将字符串转换为数字,而是希望将它们连接为字符串。
I would also like to suggest using Option Strict On, as this will help prevent errors like this from happening, as it prevent you from recompiling if you have any implicit conversions(where the compiler has to guess which type you want to convert to)
我还想建议使用Option Strict On,因为这将有助于防止发生此类错误,因为它可以防止您重新编译implicit conversions(编译器必须猜测您要转换为哪种类型)
回答by Sarvesh Mishra
use &for string concatenation. You are using +
使用&字符串连接。您正在使用+
MsgBox("You balance is " & " " & m_decBalance)
回答by rodit
You should always use the '&' symbol for connecting strings to other
您应该始终使用“&”符号将字符串连接到其他
MsgBox("You balance is " & " " & m_decBalance)

