Click here to Skip to main content
15,898,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Developing windows application using C sharp language and For back-end i am using MS Access. After executing i am getting this error.
"Could not save; currently locked by another user. "


using (OleDbConnection con = new OleDbConnection(ConfigurationManager.AppSettings["connection"]))
                   {
                       con.Open();
                       OleDbCommand cmd = new OleDbCommand("UPDATE REPORTLkgUkg set StudentPhoto=?   where RegID=? and Standard=? ", con);

                       MemoryStream ms = new MemoryStream();
                       pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
                       byte[] photo_aray = new byte[ms.Length];
                       ms.Position = 0;
                       ms.Read(photo_aray, 0, photo_aray.Length);


                       cmd.Parameters.Add(new OleDbParameter("@StudentPhoto", (Object)photo_aray));

                       cmd.Parameters.Add(new OleDbParameter("@RegID", txtRegId.Text));
                       cmd.Parameters.Add(new OleDbParameter("@Standard", cmbStd.Text));
                       cmd.ExecuteReader();

                       cmd.ExecuteNonQuery();
                       con.Close();

                   }


What I have tried:

I tried closing access file and starting it again. Don't know what to do.
Posted
Updated 24-Mar-18 1:11am

1 solution

Look at your code:
cmd.ExecuteReader();

cmd.ExecuteNonQuery();
Because you try to perform an UPDATE using a ExecuteReader, you open a OleDbDataReader on the connection - which you then discard. When you try to then execute the same code again via ExecuteNonQuery the engine checks teh Connection, finds there is a DataReader open and refuses to do the update.

Remove the call to ExecuteReader.
 
Share this answer
 
Comments
varuncodee 24-Mar-18 7:15am    
Yes that's big mistake, Thank's. i corrected it now i am using cmd.ExecuteNonQuery(); but still getting that error again.
OriginalGriff 24-Mar-18 7:18am    
Exactly where are you getting it, and what does the debugger show you?
varuncodee 24-Mar-18 8:04am    
i am getting error while saving image in database. i am getting this error "Could not save; currently locked by another user." at cmd.ExecuteNonQuery(); line.
OriginalGriff 24-Mar-18 8:14am    
Check the rest of your code: Have you got a Connection you haven't closed and disposed?

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