从字符串到双精度的转换无效

Posted

技术标签:

【中文标题】从字符串到双精度的转换无效【英文标题】:conversion from string to double is not valid 【发布时间】:2015-07-08 16:12:03 【问题描述】:

导入 mysql.Data.MySqlClient

公开课预约表

Dim MySqlConn As MySqlConnection
Dim Command As MySqlCommand
Dim Valid As Boolean = False




Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Call Input_Validation()

    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Insert into venuesdb.Event(VenueID, EventName, EventDate, EventStartTime, EventEndTime, EventID, CustomerID) Values ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & DateTimePicker1.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "', '" & TextBox6.Text & "', '" & TextBox7.Text & "')"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader

        MessageBox.Show("Booking Added")
        MySqlConn.Close()
        If TextBox1.Text = 0 Then
            MessageBox.Show("Please enter text")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()

        TextBox1.Clear()
        TextBox2.Clear()

        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBox7.Clear()
    End Try

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Delete from venuesdb.Event where EventName = '" & ComboBox1.Text & "'"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader

        MessageBox.Show("Booking Deleted")
        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()

    End Try
End Sub

Private Sub TabPage2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Select * from venuesdb.Event"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader
        While Reader.Read
            Dim Bandname = Reader.GetString("EventName")
            ComboBox1.Items.Add(Bandname)
        End While

        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()
    End Try

End Sub

Private Sub BookingForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub TabPage3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Select * from venuesdb.Event"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader
        While Reader.Read
            Dim Bandname = Reader.GetString("EventName")
            ComboBox2.Items.Add(Bandname)
        End While

        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()

    End Try
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Select * from venuesdb.Event Where EventName = '" & ComboBox2.Text & "'"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader
        While Reader.Read
            TextBox14.Text = Reader.GetString("EventName")
            TextBox13.Text = Reader.GetString("VenueID")

            TextBox11.Text = Reader.GetString("EventStartTime")
            TextBox10.Text = Reader.GetString("EventEndTime")
            TextBox9.Text = Reader.GetString("EventID")
            TextBox8.Text = Reader.GetString("CustomerID")
            DateTimePicker2.Text = Reader.GetDateTime("EventDate")

        End While

        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()

    End Try
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "Server = Localhost; database = venuesdb; user id = root; Password = "
    Dim Reader As MySqlDataReader

    Try
        MySqlConn.Open()
        Dim Query As String
        Query = "Update venuesdb.Event set EventName = '" & TextBox1.Text & "', VenueID = '" & TextBox2.Text & "', EventDate = '" & DateTimePicker2.Text & "'where EventName = '" & TextBox1.Text & "'"
        Command = New MySqlCommand(Query, MySqlConn)
        Reader = Command.ExecuteReader

        MessageBox.Show("Booking Updated")
        MySqlConn.Close()

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()

    End Try

    TextBox1.Clear()
    TextBox2.Clear()

    TextBox4.Clear()
    TextBox5.Clear()
    TextBox6.Clear()
    TextBox7.Clear()
End Sub

我有一个错误,当我单击一个按钮时不断弹出错误提示“从字符串“转换为双精度类型无效” 如您所见,我没有尝试将任何内容转换为双精度

请帮忙

【问题讨论】:

你确定这是抛出异常的地方吗?没有其他课程可以转换为Double 不,我已经检查了所有表格,但我还没有将任何东西转换成双倍? Switch Option Strict On 开始... 【参考方案1】:

这将隐式尝试转换为 Double 以尝试运行比较:

If TextBox1.Text = 0 Then
    MessageBox.Show("Please enter text")
End If

您可能希望将其(以及任何其他事件)包装在

If Textbox1.Text <> "" Then
    If TextBox1.Text = 0 Then
        MessageBox.Show("Please enter text")
    End If
End If

或者,也许更好,将其更改为:

If TextBox1.Text.Length = 0 Then 
    ....

If TextBox1.Text = "" Then
    ....

比较长度不会在比较之前尝试将其转换为双精度数。

【讨论】:

非常感谢,我放错地方了

以上是关于从字符串到双精度的转换无效的主要内容,如果未能解决你的问题,请参考以下文章

变体浮点到双精度值转换,舍入到小数点后 1 位 [重复]

C ++ OpenCV,IplImage到双数组

从 ByteBuffer 到双数组

将小数位移动到双精度

将字符串从组合框转换为双精度

是否可以将两位小数分配给双精度