Results 1 to 4 of 4

Thread: Can’t save an Imported Excel sheet to Database in VB 2005

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    54

    Can’t save an Imported Excel sheet to Database in VB 2005

    Hi guys
    I have this program that imports and Excel spreadsheet to a database, but when I try and save the imported information, nothing is saved.
    Here is my code I have so far….

    VB Code:
    1. Private Sub bindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
    2.         If Me.Validate Then
    3.             Me.StockBindingSource.EndEdit()
    4.             Me.StockTableAdapter.Update(Me.StockDataSet.Stock)
    5.         Else
    6.             System.Windows.Forms.MessageBox.Show(Me, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning)
    7.         End If
    8.     End Sub
    9.  
    10.    Private Sub ImportBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImportBtn.Click
    11.         Dim DS As System.Data.DataSet
    12.         Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter
    13.         Dim MyConnection As System.Data.OleDb.OleDbConnection
    14.  
    15.         MyConnection = New System.Data.OleDb.OleDbConnection( _
    16.               "provider=Microsoft.Jet.OLEDB.4.0; " & _
    17.               "data source=D:\Stock.XLS; " & _
    18.               "Extended Properties=Excel 8.0;")
    19.         ' Select the data from Sheet1 of the workbook.
    20.  
    21.         MyAdapter = New System.Data.OleDb.OleDbDataAdapter( _
    22.               "select * from [Sheet1$]", MyConnection)
    23.         DS = New System.Data.DataSet()
    24.         MyAdapter.Fill(DS, "StockDataGridView")
    25.         MyConnection.Close()
    26.         StockDataGridView.DataSource = DS.Tables("StockDataGridView") 'Tablegrid Name
    27.     End Sub



    Any ideas why this is not saving?

    Thanks guys

    Nick

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Can’t save an Imported Excel sheet to Database in VB 2005

    Your only populating your datasset. You need to do an Insert action to place the data into your database.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    54

    Re: Can’t save an Imported Excel sheet to Database in VB 2005

    Thanks for that RobDog888, how and were would i use the Insert?

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2005
    Posts
    54

    Re: Can’t save an Imported Excel sheet to Database in VB 2005

    This is what i have so far, but i get and error say "Object reference not set to an instance of an object."
    VB Code:
    1. Try
    2.             MyConnection.Open()
    3.             Dim command As String
    4.             ' write the transaction to the customers database if it is valid
    5.             command = "insert into StockDataGridView(ID, Code, Name, CatID, Category, SubCatID, SubCatergory, Price, Url)"
    6.             'put the sql string into the adapter
    7.             MyAdapter.InsertCommand.CommandText = command
    8.             'do the insert
    9.             MyAdapter.InsertCommand.ExecuteNonQuery()
    10.         Catch exceptionObject As Exception
    11.             MessageBox.Show(exceptionObject.Message)
    12.         Finally
    13.             MyConnection.Close()
    14.         End Try

    Thanks for your help

    Nick
    Last edited by nickj; Jan 1st, 2006 at 04:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width