Vb.net Could not save, locked by another user - vb.net

I am having the problem that about when i try to update a picture, and i click save the error message said that "could not save, locked by another user. where error i did? Sometimes I could update, but sometimes not, why?
And I realize that I have to open Access file and close it then will work.
The error line is cmd.ExecuteNonQuery().
Dim con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =..\room.accdb")
Dim ms As New MemoryStream
Dim arrimg As Byte()
Me.PictureBox1.Image.Save(ms, Imaging.ImageFormat.Png)
arrimg = ms.GetBuffer()
ms.Read(arrimg, 0, ms.Length)
Dim sql As String = "UPDATE Userss SET profilepicture =#profilepicture WHERE studentid=" & Form1.txtStuID.Text & ";"
Dim cmd As New OleDbCommand
con.open()
cmd = New OleDbCommand(sql, con)
Dim photo As OleDbParameter = New OleDbParameter("#profilepicture", SqlDbType.Image)
photo.Value = arrimg
cmd.Parameters.Add(photo)
cmd.ExecuteNonQuery()
MessageBox.Show("Profile picture saved.")
con.close()

The 'locked by another user' is a vague message by MS Access since it can be due to a couple of different reasons. It's not to be confused by a specific reason (such as always meaning a table lock). It can be that or it can be the *.ldb file itself or it can be form/table design or even permissions related. However, in your scenario, you are getting it since you have opened the db on MS Access as you mentioned. Opening the db / tables, usually put a lock on the tables.
When you open a record or the form (or table structure) is not designed optimally or someone opens the mdb/accdb who doesn't have both read and write permissions to the folder and file. This error has to do with the *.ldb file associated with the *.mdb/accdb file which is actually what is 'locking' and causing the error.
Some options can be considered, like;
using a NO LOCK in your update statement, but that might not be the best one.
make sure all your connections are well disposed after use.
use using blocks for your objects that implement IDisposable.
you can also read a bit about DEADLOCKS, it might help avoid locks

This should work:
Dim arrimg As Byte()
Using ms As New MemoryStream
Me.PictureBox1.Image.Save(ms, Imaging.ImageFormat.Png)
arrimg = ms.GetBuffer()
ms.Read(arrimg, 0, ms.Length)
End Using
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =..\room.accdb")
con.Open()
Using cmd As New OleDbCommand("UPDATE Userss SET profilepicture = #profilepicture WHERE studentid = #studentId;", con)
Dim studentID As New OleDbParameter("#studentId", Form1.txtStuID.Text)
cmd.Parameters.Add(studentID)
Dim photo As New OleDbParameter("#profilepicture", SqlDbType.Image)
photo.Value = arrimg
cmd.Parameters.Add(photo)
cmd.ExecuteNonQuery()
MessageBox.Show("Profile picture saved.")
End Using
con.Close()
End Using
From the code i can see you mix presentation and data access logic, you should probably limit each of your feature to DAL methods so it's easier to find where your DB connection stay open.
With Using like this you can easily avoid to manually dispose the objects and you can be sure the connection will be closed even with exceptions.
Always use parameters and check for disposable object (like MemoryStream).

Related

Additional information: There is already an open DataReader associated with this Command which must be closed first. vb.net

Dim cat As New Catalog()
Dim con As New OleDbConnection()
Dim cmd As New OleDbCommand
Dim ds1 As New DataSet
Dim conn As ADODB.Connection
' Open the Access database.
conn = New Connection
conn.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=" + openExcel + "\Test" + ".mdb; Persist Security Info=False"
con.ConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=" + openExcel + "\Test" + ".mdb; Persist Security Info=False"
conn.Open()
xlWorkSheet1.Columns(5).Insert
Dim cellValue As String = ""
Dim newValue As String = ""
Dim sh1 As String = ""
Dim qty As String = ""
Dim matchText As String = ""
Dim sql As String = ""
con.Open()
sh1 = LTrim$(xlWorkSheet1.Cells(i, 1).Text)
sql = "SELECT Num_ber, Q_ty FROM good WHERE Na_me LIKE 'staff%' And Ty_pe = 'ORD'"
Dim cmd As New OleDbCommand(sql, con)
Dim myReader As OleDbDataReader = cmd.ExecuteReader()
conn.Execute(sql)
myReader = cmd.ExecuteReader() ' HERE'S THE PROBLEM
xlWorkSheet1.Cells(1, 5) = myReader.GetString(0)
xlWorkSheet1.Cells(1, 11) = myReader.GetString(1)
myReader.Close()
conn.Close()
conn = Nothing
**I wanted to retrieve a specific value from mdb and then write it to excel.
Here's my code, I got this error so many times and I can't find it out. Can anybody help me? Thanks.**
[1]: https://i.stack.imgur.com/6Fuvg.png
Ok, you first have to decide when usng .net what "provider" you are going to use, AND THEN decide what kind of data objects you want to use.
You can use a oracle provider, a sql server provider, or in this case, since we using Access, then we can use EITHER oleDB, or ODBC. Either choice is fine. Most use oleDB providers for Access, but often ODBC is a good choice, especially if down the road you plane to swap out Access for say SQL server.
What the above means in plain English?
You don't want to adopt the external ADODB code and library. That code is NOT .net, and thus you REALLY but REALLY do not want to write your .net code that way. ADODB was written LONG before .net, and is what we call un-managed code (non .net). I strong, but strong suggest you do NOT add a reference to ADODB to your project, and I beyond strong recommend you avoid introduction of a non .net library for doing this!!! We certainly can adopt the oleDB provider in .net, but we will NOT have a direct reference to JET/ACE (the access database engine) in our applcation. As noted, there are some exceptions to this suggesting, but they don't apply to you and here.
Next up:
The design pattern in .net is to create the connection, get the data, and CLOSE the connection. This "pattern" will then be 100% sure that the data base is always closed, and you NEVER have to worry about if the connection is open, closed, or even if you forgot to close the connection!!! So, do this correct, and some "open" connection will never bite you, or will you have to worry about this issue.
You can in some operations keep the connection open for performance, but lets learn to walk before we run so to speak.
next up:
We certainly do NOT want to place and have connection strings all over in our code. Not only is this going to wear out your keyboard, but if you ever need to change the connection, then you going to have to hunt down all that code.
Best to let Visual Studio save that connection in ONE location, and MORE important MANAGE this for you!!!
Next up:
Do you ONLY need to work with mdb files, or do you plan/need to work with accDB files? This is a HUGE issue, and one that you cannot ignore.
next up:
Are you going to use the x32 bit version of the Access database system, or the x64 bit version?
Since your example posted code uses JET (access data engine for mdb files ONLY x32 bit version)?
Then this ALSO means you MUST (and I repeat MUST) force your .net project to run as x32 bits. You cannot use "any cpu", and you cannot use x64 bits, you MUST choose x86 bit size for your .net project. Failure to do so will result in the project not working.
Ok, with the above information?
First up, force/set/be 100% sure your project is set to run as x32 bits.
That setting is this one:
and remove the reference you have to ADO if you created one.
Ok,
next up:
Create the connection to the database.
Project ->properties.
This here:
And then:
and then
Now, you can browse, and select the access mdb file.
But, you MUST not skip the next step - you have to choose JET (older, mdb files), or choose the newer ACE (for accDB format files).
So, this:
now this:
As noted, you choose JET or ACE here.
now, we have this and you can use test connection.
BUT BE VERY careful!!!!
If you are using vs2022, then keep in mind vs2022 is the FIRST version of VS that is now x64 bits. As a result, vs can't pass the test connection!!! Your connection is in fact ok, but will fail with vs2022.
If you using a previous version of VS (before 2022), then the test connection button should work. and you see this:
Ok, now that we have a valid working conneciton setup, we can now write code, and we will NOT use ADODB!!!!
The typical code pattern to read and load a data table (like a access VBA recordset) will be like this:
Now, I became RATHER tired of writing that same using block over and over. So, in a global module, I have this code now:
Public Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New OleDbConnection(My.Settings.AccessDB)
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
So, now with the above handy dandy helper routine?
Your code becomes this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Sql As String =
"SELECT Num_ber, Q_ty FROM good WHERE Na_me LIKE 'staff%' And Ty_pe = 'ORD'"
Dim rstData As DataTable = MyRst(Sql)
Debug.Print("Na_me is " & rstData.Rows(0).Item("Na_me"))
End Sub
Or, display all return rows from that data table
Debug.Print("Na_me is " & rstData.Rows(0).Item("Na_me"))
For Each OneRow As DataRow In rstData.Rows
Debug.Print("na_me = " & OneRow("Na_me"))
Next
So, you really don't need (or want) a reader anyway. Just load the results into a nice clean, easy to use data table, and from that you can loop the table, grab rows, or do whatever you want.

How can I allow multiple users to access/edit a microsoft access database?

Now I know right off the bat that Microsoft access isn't the ideal client for multiple users accessing it but it's the only one I've got right now. I have built a small program as a sort of inventory management system. There are currently three users that will be using it regularly and at the same time. One issue I am running into with this is that sometimes the database will not be accessible and will give an error stating that the file is already in use by "so and so" user. The other issue is that I'm getting a similar error every now and then where it states "The database has been placed in a state by user on machine that prevents it from being opened or locked". I am connecting to the database through an ACE OLEDB connection using the line below
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Tool & Cutter Grinding\Tool Cutter Database.accdb;Persist Security Info = False"
I have also changed some of the settings in the actual access database such as:
Enable all macros
Add the folder the database is in to the trusted locations list
Confirm that the database is set to open in shared mode by default
I don't know if there is something small I've missed or a setting I need to change but as of yet, the problem is still persisting.
Below is an example of how I am using the database. I am using string based SQL commands but am not too familiar with DataSet/DataTable/etc. items, so I may be doing something incorrectly.
'close connection from any previous session
con.Close()
'clear dataset so as not to append data
ds.Clear()
'Select SQL query that selects ALL records from a table
Dim str As String = "SELECT * FROM " & "[" & table & "]" & ""
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=P:\Tool & Cutter Grinding\Tool Cutter Database.accdb;Persist Security Info = False"
'use try catch statement to open the connection
Try
con.Open()
Catch ex As Exception
MsgBox(Convert.ToString(ex))
End Try
'use try catch statement to add a table (dt) to the dataset (ds) in order to store values
Try
ds.Tables.Add(dt)
Catch ex As Exception
End Try
'create new dataadapter object using the sql string from above and the connection created above
da = New OleDbDataAdapter(str, con)
'create new command builder in order to excecute the SELECT SQL statement using the dataadapter created (da)
'specify prefix and suffix for cb
Dim cb = New OleDbCommandBuilder(da) With {
.QuotePrefix = "[",
.QuoteSuffix = "]"
}
'use try catch statement to fill the datatable (dt) using the dataadapter (da)
Try
da.Fill(dt)
Catch ex As Exception
MsgBox(Convert.ToString(ex))
End Try
'set the datasource of the datagridview to the datatable
dgv.DataSource = dt.DefaultView
'close the connection to the database
con.Close()
Go to your Back-End Access DB file. File > Options > Client Settings. For your Use Case No Locks should be fine, but Edited record setting will work as well if you need it
but its [sic] the only one I've got right now
Actually, it's not.
Have a look at SQL Server Compact. It's free, it's small and it handles multiple users with aplomb.
You can add all the references you need using NuGet.

does a code behind datasource and connection need to be closed after use?

does a code behind datasource and/or a connection need to be closed after databind() ? my code is vb.net
Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("DefaultDataBaseServer").ConnectionString)
conn.Open()
Dim sql As String = lblConcept.Text.Replace("#xxxxxx", xxxxxID.Value)
Dim myCommand As SqlCommand = New SqlCommand(sql, conn)
ckConceptList.DataSource = myCommand.ExecuteReader()
ckConceptList.DataBind()
does a code behind datasource and/or a connection need to be closed after databind()
The terms "code behind" and DataBind() hint that this is related to some web technology, but there arent any such tags.
In general however, anything which has a Dispose() method indicates that it likely allocates some resource under the hood which should not only be closed, but Disposed as well to release those resources.
Graphics objects like pens, brushes and Bitmaps which are not disposed can (will!) eventually raise the notorious "Generic Error in GDI+" exception. But DB objects such as Connections and Command objects left undisposed can cause your app to leak. This question was creating new DBCommand objects in a For Loop without disposing of them and encountered a System resources exceeded exception.
NET Using blocks (using() in C#) make it simple to close and dispose of these things. The following will also show how to use Parameters rather than String.Replace() on the SQL:
Dim SQL = "SELECT a, b, c FROM SomeTable WHERE Foo = #name"
Dim dt As New DataTable
Using dbcon As New MySqlConnection(MySQLConnStr)
Using cmd As New MySqlCommand(sql, dbcon)
cmd.Parameters.Add("#name", MySqlDbType.Text).Value = lblConcept.Text
dbcon.Open()
dt.Load(cmd.ExecuteReader())
dgv2.DataSource = dt
End Using ' dispose of cmd
End Using ' dispose of dbcon
String.Replace wont protect against even simple things like embedded ticks as in Carol's Cookie Shoppe
Parameters.Add() allows you to specify the data type and avoid data type errors when you leave it to VB to guess
Using declares (Dim) a new variable and initializes it, so there cannot be other dbCon or cmd variables in the same scope. It also creates a new Block Scope: Those target variables and anything declared inside it, will be local to that block. The above declares the datatable outside the block so it can be used elsewhere. You can stack or combine Usings to reduce indentation like so:
Using dbcon As New MySqlConnection(MySQLConnStr),
cmd As New MySqlCommand(sql, dbcon)
dbcon.Open()
dt.Load(cmd.ExecuteReader())
dgv2.DataSource = dt
End Using
The VB editor is a little quirky about that - intellisense wont help with the dbcon variable parameter for cmd until you finish and close the block.
More information:
Connection Pooling describes how NET minimizes the cost of creating DBConnections.
Using Statement (Visual Basic). From which, quote:
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
IDisposable is (usually) the way the Dispose method is implemented. The Remarks are well worth reading.
Avoiding Problems with the Using Statement explains the whys and wherefores of combining a Try/Catch block with a Using statement. There are also plenty of posts here describing how to use the two in combination in various situations.
How do I create a parameterized SQL query? Why Should I?
You should use a try catch and finally.
try
Dim conn As SqlConnection = New SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings("DefaultDataBaseServer").ConnectionString)
conn.Open()
Dim sql As String = lblConcept.Text.Replace("#xxxxxx", xxxxxID.Value)
Dim myCommand As SqlCommand = New SqlCommand(sql, conn)
ckConceptList.DataSource = myCommand.ExecuteReader()
ckConceptList.DataBind()
Catch ex As Exception
Finally
If pConn.State = ConnectionState.Open Then
pConn.Close()
End If
End Try

Opening SQL connection

I have inherited software to maintain. The previous version used a third party Datagridview substitute that isn't compatible with versions of Windows from Vista on. In attempting to put Datagridviews in I have run into a problem with connecting to the database.
I am trying to make a small program to play around with connection and SELECT outside of the original software so I can further understand what I am doing and without going through the full process of using the original software to get to the testing point.
Private Shared Function GetData(ByVal sqlCommand As String) As DataTable
Dim table As New DataTable()
Dim connectionString As String = "Data Source=.\SQLExpress;Integrated Security=true;" _
& "AttachDbFilename=C:blah\blah\blah.mdf;User Instance=true;"
Using con = New SqlConnection(connectionString)
Using command = New SqlCommand(sqlCommand, con)
Using da = New SqlDataAdapter(command)
da.Fill(table)
End Using
End Using
End Using
Return table
End Function
My SQL command is a simple "Select * FROM Setup" and the rest of the program is form loads, imports, and DataGridView formatting. I don't think it affects the SQL part and would be cumbersome to include here.
This results in what appears to be a closed connection.
![Connection Property] http://i.imgur.com/b5V3Qy5.png
This is a screenshot of my SQLExpress which might help diagnose connection problems.
![SQL Properties] http://i.imgur.com/bakBq5D.png
I've blurred out the computer name in grey, but I did notice that there was another computer name in pink. I don't know what it means other than maybe this database was originally created on another computer and has been copied and pasted.
Finally this is the connection string that the original software used:
"Data Source=.\SQLExpress;AttachDbFilename=C:\blah\blah\blah.mdf;Trust_Connection=Yes;"
I have also tried:
"Data Source=.\SQLExpress;AttachDbFilename=C:\blah\blah\blah.mdf;Trusted_Connection=Yes;User Instance=true"
Finally, this is my exception:
"An attempt to attach an auto-named database for file C:\blah\blah\blah.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
I got my alternate connection strings from www.connectionstrings.com.
You are missing the Open() command.
con.Open()
Full code listing. (Also a good idea is to wrap your code in a Try.... End Try block).
Private Shared Function GetData(ByVal sqlCommand As String) As DataTable
Dim table As New DataTable()
Dim connectionString As String = "Data Source=.\SQLExpress;Integrated Security=true;" _
& "AttachDbFilename=C:blah\blah\blah.mdf;User Instance=true;"
Using con = New SqlConnection(connectionString)
conn.Open()
Using command = New SqlCommand(sqlCommand, con)
Using da = New SqlDataAdapter(command)
da.Fill(table)
End Using
End Using
End Using
Return table
End Function

Cannot connect to dbf file

I'm trying to connect to to a foxpro table (.dbf) from a test vb.net form.
I recieve an OleDbException with the message "Cannot open file c:\emp\emptbl.dbf"
Have tried with both of the following connection strings:
Provider=VFPOLEDB.1;Data Source=C:\emp\emptbl.dbf
from the MSDN article here
Provider=vfpoledb;Data Source=C:\emp\emptbl.dbf;Collating Sequence=machine;
from connectionstrings.com
The latter seems to be the type to use when connecting to a single table, but the same exception is thrown regadless of which is used.
I can open and perform the same query okay in visual foxpro 6.0.
Here's my code:
Dim tbl As DataTable = New DataTable()
Using con = New OleDbConnection(conString)
cmd = New OleDbCommand() With {.Connection = con, .CommandType = CommandType.Text}
Dim sSQL As String = "SELECT * FROM(EMPTBL)"
cmd.CommandText = sSQL
Dim adp As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
con.Open()
adp.Fill(ds)
con.Close()
If (ds.Tables.Count > 0) Then
tbl = ds.Tables(0)
End If
End Using
Return tbl
The OleDB provider should only connect to the PATH where the tables are... not the actual file name. Once you connect to the PATH, you can query from ANY .Dbf file that is located in it
Provider=VFPOLEDB.1;Data Source=C:\emp
select * from emptbl where ...
You can also look at other connection string settings at
ConnectionStrings.com
UPDATE per comment.
It appears you are getting closer. with your attempt without the () parens. In VFP, within parens, it interprets that as "look for a variable called EMPTBL", and from its value is the name of the table to query from. Not something you would need to apply via OleDB connection.
Now, cant open the file is another. Is it POSSIBLE that another application has the table open and the file is in exclusive use? and thus can not be opened by the .net app too? Even for grins, if you open VFP, and just do a simple create table in the C:\Emp folder and put a single record in it, then you know no other program will be using it as it is a new file. Quit out of VFP and try to query THAT table. There should be no locks, no other program is expecting it, so it should never be opened by anything else and you should be good to go.