Check my error,,while I'm reading an excel file RRS feed

  • Question

  • User-859173698 posted

    Dear all,,

    I have been used this code but its doesn't work,,

    this code should read an excel file then store it into a table in my database (SQLserver DB),, 

    could you help,,

    thanks in advance,,

    Imports System.Data

    Partial Class _DefaultInherits System.Web.UI.Page


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim MyConnection As System.Data.OleDb.OleDbConnection

    Dim DtSet As System.Data.DataSet

    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

    MyConnection = New System.Data.OleDb.OleDbConnection _

    ("provider=Microsoft.Jet.OLEDB.4.0; Data Source='c:\ADMIN.xls'; Extended Properties=Excel 8.0;")

    MyCommand = New System.Data.OleDb.OleDbDataAdapter _

    ("select * from [Sheet1$]", MyConnection)

    MyCommand.TableMappings.Add("Table", "TestTable")

    DtSet = New System.Data.DataSet


    GridView1.DataSource = DtSet.Tables(0)


    Catch ex As Exception


    End Try

    End Sub


    End Class

    Wednesday, May 7, 2008 10:38 AM


All replies

  • User1485238302 posted

    Do you get any arror?

    Wednesday, May 7, 2008 10:54 AM
  • User-859173698 posted


    Do you get any arror?

    Sorry maybe "Check my error " wasn't clear enough

    What I want is to ask ,"why the code is not working with me"?

    Its should import the data from the excel file into a  table in my DB, but , its  doesn't do this..

    Wednesday, May 7, 2008 12:47 PM
  • User1485238302 posted

    Its should import the data from the excel file into a  table in my DB,

    Well..thats because you are not pushing it into the db. You are just trying to display it in the gridview.


    Wednesday, May 7, 2008 1:08 PM
  • User-859173698 posted

    I appreciate you,,

    How could I push the dataset into the DB?

    Thanks in advance


    Wednesday, May 7, 2008 1:16 PM
  • User1485238302 posted

    You can use SqlBulkCopy class to export excel data to sql server database table. This class is available in System.Data.SqlClient namespace.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 7, 2008 1:39 PM
  • User-859173698 posted

    Thanks alot bullpit,,

    There isn't away to insert the dataset into the database, to just add it to my code?

    Wednesday, May 7, 2008 3:02 PM