hi so ive figured out how to close the connection using alert boxes wooo hoo
but now the issue i am having is that when the excel file is uploaded, but the tab name is wrong the error alert box i not displayed, however if the uploaded file is in the incorrect format (e.g the title of the columns is incorrect) then the error message is displayed.
please see my code below
Protected Sub ButtonView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonView.Click
importButtonEnabled = True
PanelUpload.Visible = False
PanelView.Visible = True
PanelImport.Visible = False
btnBack.Visible = True
btnBack.Enabled = True
Dim xConnstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("SiteTemplate.xls") & ";" & _
"Extended Properties=Excel 8.0"
Dim objXConn As New OleDbConnection(xConnstr)
Try
objXConn.Open()
Dim objCommand As New System.Data.OleDb.OleDbCommand("SELECT * FROM [importsites$]", objXConn)
Dim objDataAdapter As New OleDbDataAdapter()
objDataAdapter.SelectCommand = objCommand
Dim objDataSet As New DataSet()
objDataAdapter.Fill(objDataSet, "xldata")
GridView1.DataSource = objDataSet.Tables(0).DefaultView
GridView1.DataBind()
objXConn.Close()
ButtonImport.Enabled = importButtonEnabled
If ButtonImport.Enabled = False Then
lblerror.ForeColor = Drawing.Color.Red
lblerror.Text = "ERROR WITH FILE"
End If
Catch ex As Exception
Response.Write("<script LANGUAGE=JavaScript type=""text/javascript"">")
Response.Write("alert('" & ex.Message.ToString & "');")
Response.Write("</script>")
If objXConn.State = ConnectionState.Open Then
objXConn.Close()
End If
Finally
End Try
End Sub