|
-
Dec 31st, 2005, 04:46 PM
#1
Thread Starter
Member
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:
Private Sub bindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
If Me.Validate Then
Me.StockBindingSource.EndEdit()
Me.StockTableAdapter.Update(Me.StockDataSet.Stock)
Else
System.Windows.Forms.MessageBox.Show(Me, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning)
End If
End Sub
Private Sub ImportBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ImportBtn.Click
Dim DS As System.Data.DataSet
Dim MyAdapter As System.Data.OleDb.OleDbDataAdapter
Dim MyConnection As System.Data.OleDb.OleDbConnection
MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=D:\Stock.XLS; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyAdapter = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)
DS = New System.Data.DataSet()
MyAdapter.Fill(DS, "StockDataGridView")
MyConnection.Close()
StockDataGridView.DataSource = DS.Tables("StockDataGridView") 'Tablegrid Name
End Sub
Any ideas why this is not saving?
Thanks guys
Nick
-
Dec 31st, 2005, 06:00 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 31st, 2005, 09:49 PM
#3
Thread Starter
Member
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?
-
Dec 31st, 2005, 09:53 PM
#4
Thread Starter
Member
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:
Try
MyConnection.Open()
Dim command As String
' write the transaction to the customers database if it is valid
command = "insert into StockDataGridView(ID, Code, Name, CatID, Category, SubCatID, SubCatergory, Price, Url)"
'put the sql string into the adapter
MyAdapter.InsertCommand.CommandText = command
'do the insert
MyAdapter.InsertCommand.ExecuteNonQuery()
Catch exceptionObject As Exception
MessageBox.Show(exceptionObject.Message)
Finally
MyConnection.Close()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|