Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me!! It is urgent

This is my code There is not any mistake But I get error Why?
C#
string path = Server.MapPath(FileUpload1.FileName).ToString();
string str3 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0; Xml; HDR=YES\"";
OleDbConnection excelcon = new OleDbConnection(str3);
excelcon.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", excelcon);
            DataSet myDataSet = new DataSet();
            myCommand.Fill(myDataSet);
            excelcon.Close();


My excel file Sheet name is also Sheet1
Posted

1 solution

Try using the code given below. It's a working code. In the code there is a slight change in the way connection string is declared--

VB
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
    Try

        If updFile.HasFile = True Then
            Dim ExcelConn As String
            Dim fileExtension As String = Path.GetExtension(updFile.FileName)
            Dim fileName As String = Server.MapPath("~/Admin/Payroll/upload/") & Date.Now.Hour & Date.Now.Minute & Date.Now.Second & Date.Now.Millisecond & fileExtension

            If fileExtension <> ".xls" And fileExtension <> ".xlsx" Then
                tdError.InnerText = "Select Valid Excel File"
                Exit Sub
            End If

            updFile.SaveAs(fileName)

            If fileExtension = ".xls" Then
                ExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileName & ";Extended Properties='Excel 8.0;HDR=YES;'"
            Else
                ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileName & ";Extended Properties='Excel 12.0 Xml;HDR=YES;'"
            End If


            Dim dtFill As New DataTable

            Using oleDBConn As New OleDbConnection(ExcelConn)
                Dim daFill As New OleDbDataAdapter("Select * from [Attendance$]", ExcelConn)
                daFill.Fill(dtFill)
                AttendanceImport.ImportAttendance(dtFill, Session("AdminID"))
            End Using

            tdError.InnerText = "Attendance Sheet Uploaded Successfully"
        Else
            tdError.InnerText = "No file Selected"
            Exit Sub
        End If
    Catch ex As Exception
        tdError.InnerText = ex.Message
    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