2014-02-06 341 views
0

我試圖在VB中顯示從數據庫登錄的用戶餘額。但是,一旦我點擊檢查平衡按鈕,它會產生錯誤Conversion from string "Your balance is " to type 'Double' is not valid「從字符串轉換爲Double類型無效」錯誤VB.NET

我已經嘗試不同的方式從一個字符串到雙轉換,我想也許這是因爲我有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 
+0

在我的數據庫'Balance'是一個整數,它的值是200我是否需要將其更改爲雙在數據庫中,然後還是什麼? – user3275784

+0

@ user3275​​784從您的錯誤中,問題與數據庫無關(請參閱下面的我的帖子)。您可以將「Balance」列保留爲整數(除非您需要在數據庫中存儲雙倍數) – Grahamvs

回答

5

的問題是,要加入一個+的字符串。您需要用&替換+。我還建議將.ToStringm_decBalance,因爲這會告訴編譯器把m_decBalancestring,像這樣:

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

你所得到的錯誤的原因是,編譯器會嘗試將字符串轉換爲數字值+與數字一起使用。例如,下面將顯示的10值一個消息:

MsgBox("5 " + " " + 5) 

Dim Val As Integer = "20 " + 15 

將導致Val35

當你想加入的字符串,我建議使用&,因爲這告訴編譯器您不希望將字符串轉換爲數字,而是希望將它們作爲字符串加入。

我也想建議使用Option Strict On,因爲這將有助於防止這樣的錯誤發生,因爲如果你有任何implicit conversions(編譯器必須猜測你想要轉換的類型),它會阻止你重新編譯,

+0

令人敬畏的東西,歡呼的人! – user3275784

0

使用&進行字符串連接。您正在使用+

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

你應該總是使用「&」符號的字符串連接到其他

MsgBox("You balance is " & " " & m_decBalance) 
相關問題