Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am having an issue with closing the connection to my sql server when an error occurs
my program allows a user to upload an excel file with specific format and a specfific tab name, as this is the template sent out the the client, however as we all know not everyone will use the template and will send it back in the incorrect format, so when i go to upload the file it brings up and error page.

the steps i do are as follows:
-browse for the excel file to upload using FileUpload, and then click upload (this then creates a back up file saved in a different location)
-Attempt to view the file uploaded (however if the file is in the incorrect format or does not have the correct tab name an error page is presented)
-i then do is close the browsers, check the original excel document and then attempt to upload it, however as the error page does not close the connection it says that the file is open by someone else

how do i close the connection when the error page is displayed?

i am using Visual Studio 2010 and programming in VB :-)

thank you for your help
Posted
Comments
Kschuler 24-May-11 15:15pm    
You may want to post relevant code to get more help.
reid0588 25-May-11 5:38am    
sorry kschuler ive just seen your comment

1 solution

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

VB
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")

            'view the data you have uploaded in your gridview. this will check for any empty rows at this point and change the colours if necessary
            GridView1.DataSource = objDataSet.Tables(0).DefaultView
            GridView1.DataBind()
            'End If
            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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900