Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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; <br />
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
VB
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
Updated 15-Jan-13 11:14am
v3
Comments
Richard C Bishop 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 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

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:
VB
Update [My_Sheet$] Set [Specification]=? where ([Part Code] = ?)


Amended Code:
VB
Me.m_oDataAdapter.UpdateCommand = New OleDb.OleDbCommand(sUpdateSQL, Me.m_oConn)

              Me.m_oDataAdapter.UpdateCommand.Parameters.Add(&amp;quot;@Specification&amp;quot;, OleDb.OleDbType.Char, 255, &amp;quot;Specification&amp;quot;)

       Me.m_oDataAdapter.UpdateCommand.Parameters.Add(&amp;quot;@OldPartCode&amp;quot;, OleDb.OleDbType.Char, 255, &amp;quot;PartCode&amp;quot;).SourceVersion = DataRowVersion.Original
 
Share this answer
 
Comments
Richard C Bishop 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900