Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i can update access mdb

but when i add in codes sum()function then get an error like this

System.Data.OleDb.OleDbException: 'Operation must use an updateable query.'


What I have tried:

Dim UPT_OGRLIST = "UPDATE OGRENCILIST  INNER JOIN (SELECT CARKOD,SUM(BORÇ) AS BORÇ FROM HAREKETLER GROUP BY CARKOD) AS HAREKETLER  ON OGRENCILIST.CARKOD=HAREKETLER.CARKOD SET OGRENCILIST.Borç=HAREKETLER.Borç"
       Dim cm_OGRLIST As New OleDbCommand(UPT_OGRLIST, cn_access)
       Dim dr_UOGRLIST As OleDbDataReader = cm_OGRLIST.ExecuteReader
Posted
Updated 14-Oct-21 2:18am

You should not be using a data reader to perform an update. You should use OleDbCommand.ExecuteNonQuery Method (System.Data.OleDb) | Microsoft Docs[^].
 
Share this answer
 
Comments
Member 14588284 14-Oct-21 3:16am    
i' using of course
i can update table with this code
"UPDATE OGRENCILIST SET BORÇ=500"
Richard MacCutchan 14-Oct-21 3:30am    
Yes, but you are trying to use a DaTaReader for the update. You need to use an updateable query as explained in the error message.
Member 14588284 14-Oct-21 3:39am    
How can i change my codes?
Richard MacCutchan 14-Oct-21 3:57am    
Follow the link I gave you and see how to use the ExecuteNonQuery command.
Member 14588284 14-Oct-21 4:09am    
Dim UPT_OGRLIST = "UPDATE OGRENCILIST SET BORÇ=(SELECT SUM(BORÇ) FROM HAREKETLER WHERE CARKOD=OGRENCILIST.CARKOD)"
Dim cm_OGRLIST As OleDbCommand = New OleDbCommand(UPT_OGRLIST, cn_access)
'Dim dr_UOGRLIST As OleDbDataReader = cm_OGRLIST.ExecuteReader **** i removed this line
cm_OGRLIST.ExecuteNonQuery()

But same error
The "Operation must use an updateable query" error usually indicates that your database file is stored in a folder to which the current user does not have permission to write.

For example, if you have stored your database file in your application folder, and your application is installed under the "Program Files" directory, the user would need to be an administrator of the local machine, and your application would need to run elevated before you could write to the folder.

Check the NTFS permissions on the folder where your database file is stored. If it's on a network share, check the permissions on the share as well.
 
Share this answer
 
Comments
Richard MacCutchan 14-Oct-21 4:38am    
I have always understood that ExecuteReader is for queries only. So was my answer incorrect?
Richard Deeming 14-Oct-21 4:40am    
You're not wrong, but I don't think using ExecuteReader for a query that doesn't return any results would generate the error from the question.
Richard MacCutchan 14-Oct-21 5:20am    
Thanks, I need to do some more study and experimentation.
Member 14588284 14-Oct-21 4:52am    
i checed permissions thats ok.
always i can update table but with this code i get error.

"UPDATE OGRENCILIST SET BORÇ=500" --ok
"UPDATE OGRENCILIST SET BORÇ=(SELECT SUM(BORÇ) FROM HAREKETLER WHERE CARKOD=OGRENCILIST.CARKOD)" --error
Richard Deeming 14-Oct-21 4:58am    
Looks like Access doesn't support that syntax.

You could try:
UPDATE OGRENCILIST As a
INNER JOIN (SELECT CARKOD, SUM(BORÇ) As NewBORÇ FROM HAREKETLER GROUP BY CARKOD) As b
ON b.CARKOD = a.CARKOD
SET a.BORÇ = b.NewBORÇ
Dim UPT_OGRLIST = "SELECT CARKOD,TBORC,TALACAK,TALACAK-TBORC AS BAKIYE FROM " &
           "(SELECT A.CARKOD,IIF( TBORC IS NULL,0,TBORC)AS TBORC,IIF( TALACAK IS NULL,0,TALACAK)AS TALACAK FROM (SELECT CARKOD,BORÇ,ALACAK,BAKiYE AS BAKIYE FROM OGRENCILIST)A " &
           "LEFT JOIN (SELECT CARKOD,SUM(BORÇ) AS TBORC,SUM(ALACAK)AS TALACAK  FROM HAREKETLER GROUP BY CARKOD)B ON A.CARKOD=B.CARKOD )"
       Dim dc = New OleDbCommand(UPT_OGRLIST, cn_access)
       Dim rows As OleDb.OleDbDataReader
       rows = dc.ExecuteReader
       If rows.HasRows Then
           Do While rows.Read()
               Dim cm_OGRLIST As New OleDbCommand("UPDATE OGRENCILIST SET BORÇ='" & rows(1) & "',ALACAK='" & rows(2) & "',BAKiYE='" & rows(2) & "' WHERE CARKOD='" & rows(0) & "'", cn_access)
               cm_OGRLIST.ExecuteNonQuery()
           Loop
       End If



ichanged my codes like this :)
make it do -loop
 
Share this answer
 
Comments
Richard Deeming 14-Oct-21 8:57am    
You've now introduced a SQL Injection[^] vulnerability. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

Dim cm_OGRLIST As New OleDbCommand("UPDATE OGRENCILIST SET BORÇ = ?, ALACAK = ?, BAKiYE = ? WHERE CARKOD = ?", cn_access)
cm_OGRLIST.Parameters.AddWithValue("BORC", rows(1))
cm_OGRLIST.Parameters.AddWithValue("ALACAK", rows(2))
cm_OGRLIST.Parameters.AddWithValue("BAKiYE", rows(3))
cm_OGRLIST.Parameters.AddWithValue("CARKOD", rows(0))
cm_OGRLIST.ExecuteNonQuery()
Member 14588284 14-Oct-21 10:03am    
Thanks all :))

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