Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Excel VB.NET OleDb
I have an Excel 2003 file that has a table on a spreadsheet
 
I can successfully fill a datatable and a datagridview using a dataadapter
 
When I come to use the update command however I always get a concurrency error, I don't believe it's a concurrency error but that seems to be the default error messages for zero records updated
 
The connection string is
Provider=Microsoft.ACE.OLEDB.12.0;Data Source= F:\My Folder\My Sheet.xls;
Extended Properties="Excel 8.0;HDR=YES";

 
The update statement for testing is
Update [My_Sheet$] Set [Part Code]='__foo' where ([Part Code] like '*OQ2*')
 

The update code is
Public Sub Update(ByVal value As PartsList.PartsListDataTableDataTable)
 
    Try
        Me.m_oDataAdapter.UpdateCommand.Connection.Open()
        Me.m_oDataAdapter.Update(value)
 
    Catch ex As DBConcurrencyException
 
        Stop
    Finally
        If Me.m_oDataAdapter.UpdateCommand.Connection.State = ConnectionState.Open Then Me.m_oDataAdapter.UpdateCommand.Connection.Close()
 
    End Try
 
End Sub
 
Error detail is:

System.Data.DBConcurrencyException was caught
Message: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

The stack trace is:

at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at DO_Tools.PartsListDataAdapter.Update(PartsListDataTableDataTable value) in C:\Users\Me\documents\visual studio 2010\Projects\DO_Tools\DO_Tools\_DataAdapters\PartsListDataAdapter.vb:line 168

I've changed the syntax but the error persisted
 
Can anyone offer a suggestion why this is not working?
Posted 15-Jan-13 10:56am
Fawxes318
Edited 15-Jan-13 12:14pm
v3
Comments
richcb at 15-Jan-13 16:18pm
   
You need to change your syntax for the where clause regarding the like condition. SQL uses % instead of *. I don't know if that is your entire problem, but it might be some of it. It would also be helpful to post your error so we can check it out.
Fawxes at 15-Jan-13 17:15pm
   
richcb - 32 mins ago
Comment from richcb copied over:
Next time use the "Improve Question" widget to add code or anything else to your thread. I found this thread that has various solutions. You may look through them and see if any of them help you. http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/bfdb40a8-0e29-4897-8251-6368abe24516/

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I've found the solution
 
I had added parameters for all the columns to the update command but I wasn't using all of them during testing, only Specification and Part Code and even these weren't used in the update command during testing.
 
By changing the update command only adding the parameters that were referenced in the update command then the update action works fine.
 
Amended Update Command:
Update [My_Sheet$] Set [Specification]=? where ([Part Code] = ?)
 
Amended Code:
Me.m_oDataAdapter.UpdateCommand = New OleDb.OleDbCommand(sUpdateSQL, Me.m_oConn)
 
              Me.m_oDataAdapter.UpdateCommand.Parameters.Add("@Specification", OleDb.OleDbType.Char, 255, "Specification")
 
       Me.m_oDataAdapter.UpdateCommand.Parameters.Add("@OldPartCode", OleDb.OleDbType.Char, 255, "PartCode").SourceVersion = DataRowVersion.Original
  Permalink  
Comments
richcb at 15-Jan-13 17:23pm
   
Good job.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 300
2 Sergey Alexandrovich Kryukov 289
3 CPallini 235
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100