Function doesn't return a value on all code paths

  • Thread starter Thread starter Hugh Self Taught
  • Start date Start date
H

Hugh Self Taught

Guest
Hi gurus,

I have the following code to import an excel spreadsheet but I have the message that it doesn't return a value on all code paths & could return a null value. I can't spot where I need another return value perhaps? Please help point out what I'm missing. I know I could just suppress the message but that's not the way to go. This code courtesy of another developer's web site some time back. I have added options I've researched to his code ie: Dim result. All help most graciously appreciated

' Courtesy of J.C.Solvoterra
Public Function ImportFromExcel(ByVal FilePath As String, ByVal isHDR As String) As DataTable
Dim extension = IO.Path.GetExtension(FilePath)
Dim conStr As String = Nothing
Dim result = Nothing

Try

If IsFileOpen(FilePath) Then
If Not (MessageBox.Show("The File " & Chr(34) & FilePath & Chr(34) & vbCrLf & "is Currently in use by another Application!" & vbCrLf & vbCrLf & "Please close the file then Click OK else Cancel", "File in Use", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = System.Windows.Forms.DialogResult.OK) Then
Exit Function
End If
End If

Select Case extension
Case ".xls"
'Excel 97-03
conStr = EXCEL97
Exit Select
Case ".xlsx"
'Excel 07
conStr = EXCEL07
Exit Select
End Select
conStr = String.Format(conStr, FilePath, isHDR)

Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim table As New DataTable()
cmdExcel.Connection = connExcel
'Get the name of First Sheet
connExcel.Open()
Dim dtExcelSchema As New DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
connExcel.Close()

'Read Data from First Sheet
connExcel.Open()
cmdExcel.CommandText = "SELECT * From [" & SheetName & "]"
oda.SelectCommand = cmdExcel
oda.Fill(table)
connExcel.Close()

result = table
'Dim RV As ReportViewer = New ReportViewer
'RV.LocalReport.DataSources.Add(New ReportDataSource)
'RV.LocalReport.DataSources.Item(0).Value = dt

Catch ex As Exception

End Try

Return result

End Function

Continue reading...
 
Back
Top