“从字符串转换为Double类型无效”错误VB.NET

时间:2014-02-06 15:59:18

标签: mysql vb.net string double type-conversion

我正在尝试在VB中显示来自数据库的登录用户余额。但是,一旦我单击Check Balance按钮,它就会产生错误Conversion from string "Your balance is " to type 'Double' is not valid

我尝试过将它从字符串转换为double的不同方法,我想也许是因为我将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

3 个答案:

答案 0 :(得分:5)

问题是您正在使用+加入字符串。您需要将+替换为&。我还建议您将.ToString添加到m_decBalance,因为这会告诉编译器将m_decBalance视为string,如下所示:

MsgBox("You balance is " & " " & m_decBalance.ToString)

您收到错误的原因是当+与数字一起使用时,编译器会尝试将字符串转换为数值。例如,以下内容将显示值为10的消息框:

MsgBox("5 " + " " + 5)

Dim Val As Integer = "20 " + 15

将导致Val成为35

当你想加入字符串时,我建议使用&,因为这告诉编译器你不希望将字符串转换为数字,而是希望将它们作为字符串连接起来。 / p>

我还建议使用Option Strict On,因为这有助于防止此类错误发生,因为如果您有任何implicit conversions(编译器必须猜测哪个),它会阻止您重新编译要转换为的类型)

答案 1 :(得分:0)

使用&进行字符串连接。您正在使用+

MsgBox("You balance is " & " " & m_decBalance)

答案 2 :(得分:0)

你应该经常使用'&'用于将字符串连接到其他

的符号
MsgBox("You balance is " & " " & m_decBalance)