Info: I'm using Visual Studio 2010, Office 2010 x32, and am running Win7 x64. Also, I'm pretty new to VB.Net/Programming.
Intro: I'm building a program to compare Transmission Line ratings. For this to more easily tie in with other programs in the office I have built the database in access using access own ID. I'm using a DataGridView to edit individual records (Typical UPDATE, INSERT, DELETE stuff). Afterwards, the data is exported to a hidden excel spreadsheet and a bit of code does all of the coversions/comparisons and then refils the same DataGridView and does a bit of house cleaning. All of this works fantastically.
Problem: After importing the excel data none of the changes to the DataGridView data will get saved. After trying to manually save of course. Which is strange considering the save (database.update(dataset.table)) works otherwise. Also, after the excel import if I then manually change a value that was new from the import and attempt to save I get a concurrency violation. My save code and excel import/export code is below:
Save:
Private Sub BaseBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles BaseBindingNavigatorSaveItem.Click
'Caution/prompt user of save
Dim promsg = "You are attempting to make changes to the database. Do you want to continue?"
Dim protitle = "Database Protection"
Dim prostyle = MsgBoxStyle.YesNoCancel Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Exclamation
Dim proresponse = MsgBox(promsg, prostyle, protitle)
If proresponse = MsgBoxResult.Yes Then
Try
Me.Validate()
Me.BaseBindingSource.EndEdit()
'Me.TableAdapterManager.UpdateAll(Me.TieLineDatabaseDataSet)
Me.BaseTableAdapter.Update(Me.TieLineDatabaseDataSet.Base)
'Display Msg box to inform user of save
Dim savemsg = "Changes saved to database."
Dim savestyle = MsgBoxStyle.OkOnly
MsgBox(savemsg, savestyle, protitle)
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
ElseIf proresponse = MsgBoxResult.No Then
'Refresh DataTable to erase changes
refreshDataSet()
Else
'Do nothing and close msgbox
End If
End Sub
Excel Import/Code/Export:
Private Sub excelComp()
'Export DGV to excel for calculations
Dim excelApp As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workBook As Microsoft.Office.Interop.Excel._Workbook = excelApp.Workbooks.Add(Type.Missing)
Dim workSheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
'******Do you want to see the spreadsheet?******
excelApp.Visible = False
'Grab excel file from directory and open
Dim excelPath As String = My.Application.Info.DirectoryPath & "\TieLineRatingCoordinator.xlsm"
workBook = excelApp.Workbooks.Open(excelPath)
workSheet = workBook.Worksheets("Summary")
workSheet = workBook.ActiveSheet
'Clearsheet of previous data (if any), Fill sheet from DGV, run coordination script
excelApp.Run("ClearSheet")
For i As Integer = 1 To BaseDataGridView.Columns.Count
'fill in headers
workSheet.Cells(1, i) = BaseDataGridView.Columns(i - 1).HeaderText
Next
For i As Integer = 0 To BaseDataGridView.Rows.Count - 2
For j As Integer = 0 To BaseDataGridView.Columns.Count - 1
'Cells(vertical, horizontal)
workSheet.Cells(i + 2, j + 1) = BaseDataGridView.Rows(i).Cells(j).Value.ToString()
Next
Next
excelApp.Run("LineRateCoord")
'Send Excel data back into DGV for review
Dim excelConn As System.Data.OleDb.OleDbConnection
Dim excelComm As System.Data.OleDb.OleDbDataAdapter
excelConn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & excelPath & "';Extended Properties=Excel 12.0 XML")
excelComm = New System.Data.OleDb.OleDbDataAdapter("select * from [Summary$]", excelConn)
Try
excelComm.Fill(Me.TieLineDatabaseDataSet.Base)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
excelConn.Close()
End Try
'Save workbook if needed, close excel and release program/House cleaning.
workBook.Save()
workBook.Close(False)
excelApp.Quit()
releaseObject(excelApp)
releaseObject(workBook)
End Sub