VB.NET record not updating - vb.net

I'm currently trying to update my database records in Access using the code below:
Call connection()
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "Update tblAccount set BALANCE='" & txtBalance.Text & "', where ACCTNO='" & txtPin.Text & "'"
dr = .ExecuteNonQuery
End With
However, I'm getting error code: Value of type 'Integer' cannot be converted to 'OleDbDataReader'. I was able to read and delete records but I'm getting this error when I'm trying to update them.

Related

How to delete specific row in excel using vb.net

When i use this code it always getting error can you someone help about this or someone suggest. is it possible that i can delete specific data using textbox or else ?...
Sorry for my bad english :D
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ProjectBsit4B\Book1.xlsx ;Extended Properties=Excel 12.0;"
cn.Open()
With cm
.Connection = cn
.CommandText = "delete [gmail$] set [NAME]= '" & txtboxName3.Text & "', [DEPARTMENT]= '" & Cb3.Text & "', [NOTES]='" & txtboxNotes3.Text & "' Where [EMAIL]='" & txtboxEmail3.Text & "' "
.ExecuteNonQuery()
End With
You cannot delete rows with OleDb on Excel:
Although the Jet OLE DB Provider
allows you to insert and update
records in an Excel workbook, it does
not allow DELETE operations. If you
try to perform a DELETE operation on
one or more records, you receive the
following error message...
From https://social.msdn.microsoft.com
What you can do is to update every cell in the row you want to delete and set them to NULL
For the update, the query could be like this:
.CommandText = "update [gmail$] set [NAME]= '', [DEPARTMENT]= '', [NOTES]='' Where [EMAIL]='" & txtboxEmail3.Text & "' "

No value given for one or more required parameters error vb.net

no_hp = TextBox1.Text
alamat = TextBox2.Text
password = TextBox3.Text
cmd = New OleDbCommand("UPDATE [user] SET no_hp = '" & CInt(TextBox1.Text) & "',alamat = " & TextBox2.Text & ", pin ='" & CInt(TextBox3.Text) & "' WHERE id = " & id & "", conn)
cmd.Connection = conn
cmd.ExecuteReader()
i was trying to update my access database with the following error
i cant seem to see where i did wrong
i already changed the data type from the textbox to match with the data types used in the database
the no_hp and pin is integer so i converted it to Cint but it doesnt seem to work
i already tried to substitute it to a variable but still it didnt work
please tell me where i did wrong
Use Parameters to avoid SQL injection, a malious attack that can mean data loss. The parameter names in Access do not matter. It is the order that they are added which must match the order in the SQL statement that matters.
The Using...End Using statements ensure that you objects are closed and disposed even it there is an error. This is most important for connections.
You con't need to set the connection property of the command because you passed the connection in the constructor of the command.
ExcuteReader is for retrieving data. Use ExecuteNonQuery to update, insert of delete.
Private Sub UpdateUsers()
Using conn As New OleDbConnection("Your connection string")
Using cmd = New OleDbCommand("UPDATE [user] SET no_hp = ?,alamat = ?, pin =? WHERE id = ?", conn)
cmd.Parameters.Add("nohp", OleDbType.Integer).Value = CInt(TextBox1.Text)
cmd.Parameters.Add("alamat", OleDbType.VarChar).Value = TextBox2.Text
cmd.Parameters.Add("pword", OleDbType.Integer).Value = CInt(TextBox3.Text)
cmd.Parameters.Add("id", OleDbType.Integer).Value = id
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub

INSERT INTO sql code connection issues

I have successfully managed to use SELECT and DELETE SQL statements and now I am trying to use INSERT INTO. However I keep getting this error:
ExecuteNonQuery requires an open and available Connection. The
connection's current state is closed.
So I tried putting con.Open() to see if that would help and I got this error:
The ConnectionString property has not been initialized.
I was wondering if anyone knows what I have done wrong. Or just if anyone has any working code. Preferably I would like to not use parameters if that is possible because I don't understand them at all. Here is my SQL code:
Dim con As OleDb.OleDbConnection
Dim comm As OleDb.OleDbCommand
con = SQLConnect()
comm = New OleDb.OleDbCommand()
comm.CommandText = "INSERT INTO " & TableName & " (" & Column & ") VALUES (" & Value & ")"
comm.Connection = con
comm.ExecuteNonQuery()
con.Close()
Here is the connection code:
Public Function SQLConnect() As OleDb.OleDbConnection
If Connector.State = ConnectionState.Open Then
dbprovider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = NEA.accdb"
Connector.ConnectionString = dbprovider & dbsource
Connector.Open()
End If
Return Connector
End Function
Use one open connection and one close connection like this:
Open the connection
Execute all your DELETE and INSERT queries.
Close the connection.
Try this:
con.Open()
comm.Connection = con
comm.CommandText = "DELETE ..."
comm.ExecuteNonQuery()
comm.CommandText = "INSERT INTO " & TableName & " (" & Column & ") VALUES (" & Value & ")"
comm.ExecuteNonQuery()
con.Close()
BTW, I recommend that you use parameters instead of string concatenation on your sql query. To avoid sql injection.

VB.NET updating .accdb records

I'm working on a VB.Net application that interfaces with an .accdb file to create and (eventually) update records on two tables in the same database. I'm able to write new information to a table no problem, however it is updating/changing/adding additional information to that same row in the table I'm having issues with. My code for writing updates to an existing row is at the bottom of my post.
The biggest issue I'm having is, after I execute this subroutine, it fails at the objCmd.ExecuteNonQuery() with the error message IErrorInfo.GetDescription failed with E_FAIL(0x80004005). I've combed through here and Google, trying different methods and moving things around and I cannot figure out what I'm missing. As far as I can tell, I am not using any reserved words in my SQL query. The block under the Else statement does work for creating new rows (I don't have issues with that side of my program), maybe the syntax is different for doing UPDATE commands? Any help/insight is greatly appreciated.
Private Sub WriteToDatabase()
strcs = txtSerialNumber.Text
strOrderType = orderType
strPoRMA = txtPoRMA.Text
strtech = cboTech.Text
strDate = calendarTest.SelectionStart
'Write to database if Production
If strOrderType = "PO" Then
'Check database for duplicate record
strSQL = "SELECT * FROM [New Camera Database] WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
dr = objCmd.ExecuteReader
dr.Read()
If dr("calCompleteDate").ToString <> "" Then
MsgBox("Camera S/N " & strcs & " completed " & dr("calCompleteDate") & ". Use Lookup to reprint Cert. of Compliance", vbOK + vbExclamation,
"Camera S/N " & strcs & " already completed")
exitFlag = True
Else
'Write to New Camera Database Table
strSQL = "UPDATE [New Camera Database] SET poNum=#poNum , calCompleteDate=#calCompleteDate, calCompleteTech=#calCompleteTech WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("#poNum", strPoRMA)
objCmd.Parameters.AddWithValue("#calCompleteDate", strcs)
objCmd.Parameters.AddWithValue("#calCompleteTech", strtech)
objCmd.ExecuteNonQuery()
'Write to up2DateTravelers Table
strSQL = "UPDATE up2DateTravelers SET poRMANum = #poRMANum, calCompleteDate = #calCompleteDate, calCompleteTech = #calCompleteTech WHERE cameraSer=" & strcs
objCmd = New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("#poRMANum", strPoRMA)
objCmd.Parameters.AddWithValue("#calCompleteDate", strcs)
objCmd.Parameters.AddWithValue("#calCompleteTech", strtech)
objCmd.ExecuteNonQuery()
End If
ElseIf strOrderType = "RMA" Then
'Create new functions, userform, etc (TBD)
End If
btnClear.PerformClick()
End Sub
I guess this line :
objCmd.Parameters.AddWithValue("#calCompleteDate", strcs)
is a mistake and that you wanted to use the Date :
objCmd.Parameters.AddWithValue("#calCompleteDate", strDate)
Also, Use Using and parametrized queries :
'Write to New Camera Database Table
strSQL = "UPDATE [New Camera Database] SET poNum=#poNum , calCompleteDate=#calCompleteDate, calCompleteTech=#calCompleteTech WHERE cameraSer=#cameraSer"
Using objCmd As New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("#poNum", strPoRMA)
objCmd.Parameters.AddWithValue("#calCompleteDate", strDate)
objCmd.Parameters.AddWithValue("#calCompleteTech", strtech)
objCmd.Parameters.AddWithValue("#cameraSer", strcs)
objCmd.ExecuteNonQuery()
End Using
'Write to up2DateTravelers Table
strSQL = "UPDATE up2DateTravelers SET poRMANum = #poRMANum, calCompleteDate = #calCompleteDate, calCompleteTech = #calCompleteTech WHERE cameraSer=#cameraSer"
Using objCmd As New OleDbCommand(strSQL, dbconn)
objCmd.Parameters.AddWithValue("#poRMANum", strPoRMA)
objCmd.Parameters.AddWithValue("#calCompleteDate", strDate)
objCmd.Parameters.AddWithValue("#calCompleteTech", strtech)
objCmd.Parameters.AddWithValue("#cameraSer", strcs)
objCmd.ExecuteNonQuery()
End Using

Database record not getting updated

I'm hoping for someone to be able to offer me some help please? I have spent the last 4 hours trying to fix this problem but not got anywhere. I dont actually have an error code which is making it even more difficult, it just doesnt do anything.
What I am trying to do is Read the value in Textbox2 and minus it from the GamesPlayed field in Table1 where the JobID matches TextBox1.
I dont see what I have done wrong? Many thanks.
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\Database1.mdb")
Dim cmd As New OleDbCommand
With cmd
.CommandType = CommandType.Text
.Connection = conn
.CommandText = "UPDATE [Table1] SET GamesPlayed = GamesPlayed - " & Val(TextBox2.Text) & " WHERE JobID = TextBox1.text"
.Parameters.Add("#p1", Me.ComboBox1.SelectedValue)
End With
conn.Open()
cmd.ExecuteNonQuery()
You where off to a good start, but you need to use the parameters you add.
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\Database1.mdb")
Dim cmd As New OleDbCommand
With cmd
.CommandType = CommandType.Text
.Connection = conn
.CommandText = "UPDATE [Table1] SET GamesPlayed = #p1 WHERE JobID = #p2"
.Parameters.Add("#p1", Me.ComboBox1.SelectedValue)
.Parameters.Add("#p2", Me.Textbox1.Text)
End With
conn.Open()
cmd.ExecuteNonQuery()
I would also recommend naming your controls something useful and not the default designer names.
Change this:
.CommandText = "UPDATE ... JobID = TextBox1.text"
to that:
.CommandText = "UPDATE ... JobID = " & TextBox1.text
cmd.ExecuteNonQuery() will not directly return a value. If you would like to get a value, try cmd.ExecuteReader(). Example:
Dim cmd As New OleDb ("UPDATE [Table1] SETCommandGamesPlayed = GamesPlayed - " & Val(TextBox2.Text) & " WHERE JobID = TextBox1.text")
Dim yourvalvue As OleDbDataReader = cmd.ExecuteReader()
Do While (yourvalue.read)
'what you want to do
Loop
This question is a good example of what the different execute functions do.