【问题标题】:"Conversion from string to type Double is not valid" error VB.NET“从字符串转换为 Double 类型无效”错误 VB.NET
【发布时间】:2014-02-06 15:59:18
【问题描述】:

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

我尝试了不同的方法将它从字符串转换为双精度,我想可能是因为我将 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

【问题讨论】:

  • 在我的数据库中,Balance 是一个整数,它的值为 200。我需要在数据库中将其更改为双精度数还是什么?
  • @user3275​​784 根据您的错误,该问题与数据库无关(请参阅我下面的帖子)。您可以将 Balance 列保留为整数(除非您需要在数据库中存储双精度)

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


【解决方案1】:

问题是您正在使用+ 连接一个字符串。您需要将+ 替换为&。我还建议将.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

当您想要连接字符串时,我建议使用&,因为这会告诉编译器您不希望将字符串转换为数字,而是希望将它们作为字符串连接。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您应该始终使用“&”符号将字符串连接到其他

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-30
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多