Hi how can i pass if my connection is false?

i have a code here

    Dim con As New OleDbConnection
    Dim dt As New DataTable
    Dim ds As New DataSet

    con.ConnectionString = ("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & ConMonthEndLocation & "';Extended Properties=Excel 8.0;")
    con.Open()
    dt.Clear()

    ds.Tables.Add(dt)

    With Form_Month_End_Report

        .ListView_LOB.Columns.Clear()
        .ListView_LOB.Items.Clear()

        Dim da As New OleDbDataAdapter("Select * from [" & EBU1LOB & "]", con)
        da.Fill(dt)
        ' Importing all the Colum Header of excel to listview Column Header
        Dim dc As DataColumn
        For Each dc In ds.Tables(0).Columns
            Dim ch As New ColumnHeader
            ch.Text = dc.ColumnName
            ch.Width = 105
            .ListView_LOB.Columns.Add(ch)
        Next
        ' Importing all data from Excel to Listview
        For Each myRow In dt.Rows
            .ListView_LOB.Items.Add(myRow.Item(0))
            .ListView_LOB.Items(.ListView_LOB.Items.Count - 1).SubItems.Add(myRow.Item(1))
            .ListView_LOB.Items(.ListView_LOB.Items.Count - 1).SubItems.Add(myRow.Item(2))
        Next
        con.Close()

    End With

how can i insert if connection is false then msgbox("Connection failes")
else
put the avove code in here.

Dani AI

Generated

Good call by — handling the open attempt is the right place to show a failure message. The original code from works but can be made more robust: validate the Excel file path, use a Using block (so the connection is always disposed), handle provider/bitness issues (Jet is 32-bit only), and guard against missing columns or DBNull values before accessing row(0), row(1), etc. For common Excel connection strings and provider notes see Excel connection strings examples and for the ADO.NET connection object see the OleDbConnection documentation.

Suggested checklist before you run the import:

  • Confirm ConMonthEndLocation file exists with System.IO.File.Exists.
  • Use Extended Properties="Excel 8.0;HDR=Yes;IMEX=1" (or ACE provider for newer files / 64-bit).
  • Compile the app as x86 if you must use Microsoft.Jet.OLEDB.4.0, or switch to Microsoft.ACE.OLEDB.12.0.
  • Surround the open with error handling and check ConnectionState.Open before querying.
  • When populating the ListView, call BeginUpdate/EndUpdate, check dt.Columns.Count before indexing, and convert DBNull to string.

Example pattern (safe open + guards):

' validate file, open in Using and handle exceptions, then check .State = ConnectionState.Open
' check dt.Columns.Count and use If IsDBNull(row(i)) Then "" Else row(i).ToString()
' use BeginUpdate/EndUpdate around bulk ListView updates

If the connection still fails, capture the exception message (or log it) — that usually shows whether the path, provider, or sheet name is wrong. For VB Try/Catch usage see the official guide at Microsoft.

Recommended Answers

All 3 Replies

You can put an error handling exception in your code, give it try.. :)

    Try

        `Inline Code Here`

    Catch ex As OleDbException

        `connection fail message here

    End Try

Nice it works perfectly fine :) i think your also a Pinoy.

yes I am..hehe kumusta? :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.