Using DataReader in VB.NET - vb.net

I got the error message There is an open data reader associated with this command which needs to be closed first by using the following code:
myCommand = New SqlCommand("SELECT BookCode FROM tblBook",myConnection)
myReader = myCommand.ExceuteReader
While myReader.Read
If myReader(0).ToString <> txtBookCode.Text Then
myCommand = New SqlCommand("INSERT INTO tblBook VALUES(#BookCode, #BookTitle)",myConnection)
myCommand.Parameters.AddWithValue("#BookCode", txtBookCode.Text)
myCommand.Parameters.AddWithValue("#BookTitle", txtBookTitle.Text)
myCommand.ExecuteNonQuery()
Else
MsgBox("There is already a book name '"& txtTitle.Text "'. Please try another code.",vbOkOnly,"BookCode Exists")
End If
End While
Pleas help.

Don't reuse myCommand variable. Create new one.
myCommand should be disposed in the end too (as well as reader).
Real reason of exception is more likely that you're trying to run two command on one connection at the same time.
First read all data you need from reader and THEN do all inserts. Not both at once (i assume you don't want to create two connections. That would suck)

looks like you're trying to use one variable myCommand more than once - in the first line of code and within a WHILE loop.
it's better to declare one more AdoCommand variable to use it in LOOP

Related

Why wont my SET value WHERE SQL Statement not work

Currently cant get this to work, despite it being almost for verbatim the same as else where in my code.
Using con As New OleDbConnection(constring)
Using cmd As New OleDbCommand("UPDATE " & "`" & "SIQPERSIST" & "`" & " SET [Date_Added] = #Date_Added WHERE [BatchName] = #BatchName", con)
cmd.Parameters.AddWithValue("#BatchName", BatchName2)
cmd.Parameters.AddWithValue("#Date_Added", Date.Now.ToShortDateString)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
I'm working in Vb.net
and i need to update all rows that have the name BatchName2 (this comes from a textbox)
with the current date.
The table they are on is SIQPERSIST.
The error i get is that its missing a parameter.
But i have don't know what parameter it needs despite almost similar code working else where, except the working code uses a WHERE KEY= 'keynumber' statement.
The issue is this uses backticks for the concatenated variable. Remember, ` and ' are not the same thing, and only one of those would work here.
It should look like something more like this:
Using con As New OleDbConnection(constring)
Using cmd As New OleDbCommand("UPDATE SIQPERSIST SET [Date_Added] = Date() WHERE [BatchName] = #BatchName", con)
cmd.Parameters.AddWithValue("#BatchName", BatchName2)
con.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Note, there's no need to call con.Close() when you have a Using block to take care of that for you.
Additionally, not only did I convert BatchName2 to a real query parameter (Shame on you for adding concatenation to a query that already demonstrates how to use parameters!), but I was also able to convert the existing parameter to use get the date in the DB itself.

havin issues inserting data into mysql using vb.net. it's keeps telling me connection already open [duplicate]

This error keeps popping up!!! An unhandled exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
Additional information: The connection is already open.
Dim cmd As MySqlCommand
con.Open()
Try
cmd = con.CreateCommand()
cmd.CommandText = "update animal_sale set #NOAB,#Amount,#Tax,#Total where Species=#Species"
cmd.Parameters.AddWithValue("#Species", TextBoxSpecies.Text)
cmd.Parameters.AddWithValue("#NOAB", TextBoxNo.Text)
cmd.Parameters.AddWithValue("#Amount", TextBoxAmount.Text)
cmd.Parameters.AddWithValue("#Tax", TextBoxTax.Text)
cmd.Parameters.AddWithValue("#Total", TextBoxTotal.Text)
cmd.ExecuteNonQuery()
load()
Catch ex As Exception
End Try
End Sub
It looks like you are not closing the connection after executing the query. You only have
con.Open()
and are not closing the connection after
cmd.ExecuteNonQuery()
Keep your database objects local to the method where they are used. Then you always know the state of a connection and can be sure they are closed and disposed. Using...End Using blocks do this for you even if there is an error. In this code both the connection and the command are covered by a single Using block. Note the comma at the end of the first Using line.
You can pass your connection string directly to the constructor of the connection.
You can pass your command text and the connection directly to the constructor of the command.
You Update sql command is not correct. You need to tell the server what fields to update. I had to guess at the names of the fields. Check you database for the correct names and adjust the code accordingly.
Please don't use .AddWithValue. See http://www.dbdelta.com/addwithvalue-is-evil/
and
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
and another one:
https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
Here is another
https://andrevdm.blogspot.com/2010/12/parameterised-queriesdont-use.html
I had to guess at the datatypes and field size for the .Add method. Check you database for the correct values and adjust the code.
I converted the text box strings to the proper datatype here in the database code but normally these values would be parsed and converted before they reach this code.
Private Sub UpdateSale()
Using con As New MySqlConnection("Your connection string"),
cmd As New MySqlCommand("update animal_sale set nonab = #NOAB, amount = #Amount, tax = #Tax, total = #Total where species = #Species;", con)
cmd.Parameters.Add("#Species", MySqlDbType.VarChar, 100).Value = TextBoxSpecies.Text
cmd.Parameters.Add("#NOAB", MySqlDbType.Int32).Value = CInt(TextBoxNo.Text)
cmd.Parameters.Add("#Amount", MySqlDbType.Decimal).Value = CDec(TextBoxAmount.Text)
cmd.Parameters.Add("#Tax", MySqlDbType.Decimal).Value = CDec(TextBoxTax.Text)
cmd.Parameters.Add("#Total", MySqlDbType.Decimal).Value = CDec(TextBoxTotal.Text)
con.Open
cmd.ExecuteNonQuery()
End Using
End Sub

VB Access DB Update statement

I am new to this forum, please could you help me get this code to work, when i execute it, it simply does nothing and does not update the DB. If i remove the square brackets it gives an error: "SYNTAX ERROR in UPDATE statement"
Any help appreciated!
Dim connection As OleDbConnection
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=UserDB.accdb;Jet OLEDB:Database;")
connection.Open()
Dim pass As String
pass = txtconfirm.Text
Dim user As String
user = LoginForm.UsernameTextBox.Text
Dim query As String = "UPDATE [Users] SET [Password]= '" & pass & "' WHERE [Username]= '" & user & "';"
Dim command As New OleDbCommand(query, connection)
command.ExecuteNonQuery()
connection.Close()
Given your actual connection string, the database that will be updated is the one in the directory where your application starts. This means that if you work with a WinForms application this folder is \BIN\DEBUG or x86 variant. If there is not error then you could get the return value of the ExecuteNonQuery call to verify if a record has been updated or not
Dim rowsUpdated = command.ExecuteNonQuery()
MessageBox.Show("Record updated count = " & rowsUpdated)
If this value is not zero then your database has been updated and you are looking for changes in the wrong database. Check the one in the BIN\DEBUG folder.
In any case your code has big problems. If your variables user or pass contain a single quote, then your code will crash again because your string concatenation will form an invalid SQL. As usual the only workaround is to use a parameterized query
Dim pass = txtconfirm.Text
Dim user = LoginForm.UsernameTextBox.Text
Dim query As String = "UPDATE [Users] SET [Password]= #p1 WHERE [Username]= #p2"
Using connection = New OleDbConnection("...........")
Using command As New OleDbCommand(query, connection)
connection.Open()
command.Parameters.Add("#p1", OleDbType.VarWChar).Value = pass
command.Parameters.Add("#p2", OleDbType.VarWChar).Value = user
command.ExecuteNonQuery()
End Using
End Using
The parameterized approach has many advantages. Your query text is more readable, there is no misunderstanding between your code and the values expected by your database engine. And while not easy to exploit with MS-Access there is no problem with Sql Injection
I think Steve presents a much better approach for you coding this...
Let me just throw out a few more things:
The reason you can't take those brackets out is some of your column names are reserved words; just FYI.
Since you report "it does nothing..." when you execute, it sounds like you have a valid connection and sql syntax, in which case my next step would be to copy the sql command text while in debug mode, change it to a select and run it in your DB. You should get one result when you do. If not, either your criteria or field contents are not what you think they are...
Just change the Update table SET field-value ... to SELECT * FROM table and leave the WHERE clause as is.

Vb.net Could not save, locked by another user

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).

Perform Action on Each Record from SQLDataSource

I've submitted a bunch of questions as of late - but this has been a great repository of information. I'm a .NET nub, as you can see, so if I'm missing basics or information please let me know and I'll try and fill in the gaps.
I'm using ASP.NET/VB.NET to create this with SQL 2005. I have a project where I'd like to take a set of records from a table, then send each one through an API, get a result and writeback the result to a table, then move to the next.
Initially, my thought was create a SQLDataSource that grabs all the records, then perform the action on a button to do the action of sending through each record.
Is there a way I can call the recordset from SQLDataSource and perform a loop? I'm thinking something like in Classic ASP/VBScript where you would open a RecordSet, do an action, then Loop until the RS was EoF.
Thanks for the help!
You can may want to put your results in a dataset. After getting the results, you can loop through the returned rows
Dim ds As Dataset = GetSomeDataFromSq
For Each dr As DataRow In ds.Tables(0).Rows
Console.WriteLine (dr("ColName"))
Next
You could also use a sqlDataReader
Using conn As sqlconnection = New sqlconnection("put conn info")
Using MyCommand As SqlCommand = New SqlCommand("SELECT ProductName FROM products", conn)
conn.Open()
Using myDataREader As SqlDataReader = MyCommand.ExecuteReader
While myDataREader.Read
Response.Write("Name: " & myDataREader.Item("ProductName"))
End While
End Using
End Using
End Using