【问题标题】:"Conversion from string "" to type 'Boolean' is not valid."“从字符串“”到类型“布尔”的转换无效。”
【发布时间】:2015-01-19 08:00:05
【问题描述】:

如果“从字符串”转换为类型“布尔”无效,我会遇到问题。”

If xlstxtbox1.Text = "" & xlstxtbox2.Text = "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select * from [sheet1$]",cn)
                    '2
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text = "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + "from [sheet1$] ", cn)
                    '3
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] ", cn)
                    '4
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text = "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select * from [sheet1$] " + xlstxtbox3.Text, cn)
                    '5
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text = "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + "from [" + xlstxtbox2.Text + "$] ", cn)
                    '6
                ElseIf xlstxtbox1.Text = "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
                    '7
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text = "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] " + xlstxtbox3.Text, cn)
                    '8
                ElseIf xlstxtbox1.Text <> "" & xlstxtbox2.Text <> "" & xlstxtbox3.Text <> "" Then
                    cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
                End If

【问题讨论】:

  • 您为什么希望能够将空字符串转换为布尔值?也许你可以找出哪个表达式产生了错误。
  • 这是我的选择,但我的例外是此消息:“从字符串“”转换为“布尔”类型无效。”我刚刚写了这个问题,但它被删除了。我只是想得到一个答案,我该如何解决它。
  • 首先找出代码中您尝试将空字符串转换为布尔值的位置。这是在代码中的什么地方发生的?
  • nwm,抱歉耽误你的时间,我找到了这样的方法:xlstxtbox1.Text.length = 0
  • 我在问题中看不到该代码。这非常令人困惑。

标签: vba type-conversion


【解决方案1】:

这就是答案。

Private Sub xls_opn(sender As Object, e As EventArgs) 处理 openxls.Click

    Dim cmdselcet = New OleDbCommand

    Dim adapter As New OleDbDataAdapter

    Using cn As New OleDbConnection(constr)

        Try

            cn.Open()

            '1 üres,üres,üres
            If xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select * from [sheet1$]", cn)

                '2 nem,üres,üres
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] ", cn)

                '3 üres,nem,üres
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] ", cn)

                '4 üres,üres,nem
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select * from [sheet1$] " + xlstxtbox3.Text, cn)

                '5 nem,nem,üres
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length = 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] ", cn)

                '6 üres,nem,nem
            ElseIf xlstxtbox1.Text.Length = 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select * from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)

                '7 nem,üres,nem
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length = 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [sheet1$] " + xlstxtbox3.Text, cn)

                '8 nem,nem,nem
            ElseIf xlstxtbox1.Text.Length <> 0 & xlstxtbox2.Text.Length <> 0 & xlstxtbox3.Text.Length <> 0 Then
                cmdselcet = New OleDbCommand("select " + xlstxtbox1.Text + " from [" + xlstxtbox2.Text + "$] " + xlstxtbox3.Text, cn)
            End If

            adapter.SelectCommand = cmdselcet
            Dim ds As DataSet
            ds = New DataSet

            'Megjelenítés
            adapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            DataGridView1.Visible = True
            MsgBox("XLS/XLSX import kész!")

        Catch ex As Exception

            DataGridView1.Visible = False
            MsgBox(ex.Message)

        End Try

    End Using

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2013-10-31
    • 2011-07-30
    • 2015-02-09
    相关资源
    最近更新 更多