Visual Basic InputBox关闭错误

时间:2016-08-04 10:10:08

标签: vb.net error-handling inputbox

这是一个奇怪的问题,并为该问题包含的大量代码道歉,但是,我已经给了别人的项目来维护,并且用户来找我有错误。有一个按钮可以打开InputBox,如下所示。

enter image description here

表单用于输入要导入的文件的路径。如果用户没有输入路径或输入不正确的路径,则会显示错误 - 很好。现在,问题是,如果用户按下取消'按钮或右上角的x关闭表单,它也返回相同的错误,表示找不到路径。

在查看以下代码后,我无法解决如何制作它以便在按x或取消时不显示错误,那么任何人都可以帮助我吗?

 Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click

    Try
        Dim importbox As String = InputBox("Input path", "Import", "")
        Dim fi As New FileInfo(importbox)
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName

        Dim conn As New OleDbConnection(connectionString)
        conn.Open()

        Dim add1 As String = ""
        Dim add2 As String = ""
        Dim add3 As String = ""
        Dim add4 As String = ""
        Dim add5 As String = ""
        Dim postcode As String = ""
        Dim telephone As String = ""
        Dim fax As String = ""
        Dim email As String = ""
        Dim customercode As String = ""
        Dim customername As String = ""
        Dim webpage As String = ""
        Dim mobile As String = ""

        Dim headerTable As DataTable = ugHeaders.DataSource
        Dim csvArray(headerTable.Rows.Count) As String
        Dim i As Integer = 0
        For Each dr As DataRow In headerTable.Rows
            csvArray(i) = dr.Item("CSVName")
        Next

        For Each dr As DataRow In headerTable.Rows
            Select Case dr.Item("DBName").ToString.Trim
                Case "Add1"
                    add1 = dr.Item("CSVName")
                Case "Add2"
                    add2 = dr.Item("CSVName")
                Case "Add3"
                    add3 = dr.Item("CSVName")
                Case "Add4"
                    add4 = dr.Item("CSVName")
                Case "Add5"
                    add5 = dr.Item("CSVName")
                Case "PostCode"
                    postcode = dr.Item("CSVName")
                Case "Telephone"
                    telephone = dr.Item("CSVName")
                Case "Fax"
                    fax = dr.Item("CSVName")
                Case "Email"
                    email = dr.Item("CSVName")
                Case "Customer_Name"
                    customername = dr.Item("CSVName")
                Case "Customer_Code"
                    customercode = dr.Item("CSVName")
                Case "webpage"
                    webpage = dr.Item("CSVName")
                Case "mobile_phone"
                    mobile = dr.Item("CSVName")
            End Select
        Next

        Dim sqlSelect As String = "SELECT Company, [" & add1 & "], [" & add3 & "], [" & postcode & "], [" & add2 & "], " & _
                                          "[" & telephone & "], [" & fax & "], [" & email & "], [" & customercode & "], " & _
                                          "[" & add4 & "], [" & add5 & "], [" & webpage & "], [" & mobile & "] FROM " & fi.Name

        Dim cmdSelect As New OleDbCommand(sqlSelect, conn)

        Dim adapter1 As New OleDbDataAdapter(cmdSelect)

        Dim ds As New DataSet
        adapter1.Fill(ds, "DATA")

        pb_progress.Maximum = ds.Tables(0).Rows.Count
        pb_progress.Value = 0

        For Each dr As DataRow In ds.Tables(0).Rows
            Try
                Debug.WriteLine(dr.Item(customercode).ToString.Trim)

                If dr.Item(customercode).ToString.Trim = "" Then
                    Dim str As String = dr.Item(customername)
                    If str.Trim = "" Then Continue For
                    Dim length As Integer = str.Length
                    If length < 20 Then
                    Else
                        length = 20
                    End If

                    str = Replace(str.Substring(0, length), " ", "_").ToUpper
                    str = Regex.Replace(str, "[^a-zA-Z _&]", "")

                    Dim found As Boolean = True
                    Dim loopcount As Integer = 1

                    Do Until found = False
                        Dim checkSql As String = "SELECT * FROM Customers WHERE [Customer_Code] = @ccode"
                        Dim checkCmd As New OleDb.OleDbCommand(checkSql, con)
                        checkCmd.Parameters.AddWithValue("@ccode", str)
                        Dim checkDa As New OleDb.OleDbDataAdapter(checkCmd)
                        Dim checkDt As New DataTable
                        checkDa.Fill(checkDt)

                        If checkDt.Rows.Count <> 0 Then
                            found = True
                            str &= CStr(loopcount)
                            loopcount += 1
                        Else
                            found = False
                        End If
                    Loop

                    dr.Item(customercode) = str
                Else
                    Dim found As Boolean = True
                    Dim loopcount As Integer = 1
                    Do Until found = False
                        Dim checkSql As String = "SELECT * FROM Customers WHERE [Customer_Code] = @ccode"
                        Dim checkCmd As New OleDb.OleDbCommand(checkSql, con)
                        checkCmd.Parameters.AddWithValue("@ccode", dr.Item(customercode))
                        Dim checkDa As New OleDb.OleDbDataAdapter(checkCmd)
                        Dim checkDt As New DataTable
                        checkDa.Fill(checkDt)

                        If checkDt.Rows.Count <> 0 Then
                            found = True
                            dr.Item(customercode) &= CStr(loopcount)
                            loopcount += 1
                        Else
                            found = False
                        End If
                    Loop
                End If

                Dim sql As String
                sql = "INSERT INTO Customers(Customer_Code, Customer_Name, Contract_Payment_Terms, Aq_Date, Telephone, Fax, Email, Average_Payment_Terms, webpage, mobile_phone) " & _
                    "VALUES(@ccode, @cname, 30, #01/01/2016#, @ctele, @cfax, @email, 30, @webpage, @mobile);"
                Dim cmd As New OleDb.OleDbCommand(sql, con)
                With cmd.Parameters
                    .AddWithValue("@ccode", dr.Item(customercode))
                    .AddWithValue("@cname", dr.Item(customername))
                    .AddWithValue("@ctele", dr.Item(telephone).ToString.Truncate(48))
                    .AddWithValue("@cfax", dr.Item(fax))
                    .AddWithValue("@email", dr.Item(email))
                    .AddWithValue("@webpage", dr.Item(webpage))
                    .AddWithValue("@mobile", dr.Item(mobile))
                End With
                cmd.ExecuteNonQuery()

                sql = "INSERT INTO [Customer_Addresses] (Cust_Code, PostCode, Alias, Add1, Add2, Add3, Add4, Add5) VALUES(@ccode, @pcode, 'Default'" & _
                    ",@add1, @add2, @add3, @add4, @add5);"
                cmd = New OleDb.OleDbCommand(sql, con)
                With cmd.Parameters
                    .AddWithValue("@ccode", dr.Item(customercode))
                    .AddWithValue("@pcdoe", dr.Item(postcode))
                    .AddWithValue("@add1", dr.Item(add1))
                    .AddWithValue("@add2", dr.Item(add2))
                    .AddWithValue("@add3", dr.Item(add3))
                    .AddWithValue("@add4", dr.Item(add4))
                    .AddWithValue("@add5", dr.Item(add5))
                End With
                cmd.ExecuteNonQuery()

            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try


            pb_progress.Increment(1)
        Next

        MsgBox("Import successful", MsgBoxStyle.OkOnly, "Success")

    Catch ex As Exception
        errorLog(ex)
    End Try
End Sub

1 个答案:

答案 0 :(得分:2)

Inputbox始终return一个String

  • 如果用户按下&#34; OK&#34;它会将String放回TextBox
  • 如果他按X或取消returns ""取消该框。

我通常不建议使用Inputbox来获取文件路径。请改用OpenFileDialog。如果您的Clipboard中已有完整路径,则可以将其粘贴到Filename-Textbox的{​​{1}}中,然后按Enter键。

这应该让你开始:

OFD