I Cannot Update my database - sql

Whats wrong with these?
My module is:
Imports System.Data.OleDb
Module Module1
Public con As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\CITeval\system7\system7\evaluation.mdb")
Public da As OleDbDataAdapter
Public dr As OleDbDataReader
Public cmd As OleDbCommand
Public ds = New DataSet
Public CurrentRow As Integer
Public sql As String
End Module
btn update
Try
Dim Str As String
Str = "update studentsrecord set IDNumber="
Str += """" & txtIDNumber.Text & """"
Str += " where IDNumber="
Str += txtIDNumber.Text.Trim()
con.Open()
cmd = New OleDbCommand(Str, con)
cmd.ExecuteNonQuery()
con.Close()
con.Open()
Str = "update studentsrecord set FirstName="
Str += """" & txtfirst.Text & """"
Str += " where IDNumber="
Str += txtIDNumber.Text.Trim()
con.Open()
cmd = New OleDbCommand(Str, con)
cmd.ExecuteNonQuery()
con.Close()
con.Open()
Str = "update studentsrecord set LastName="
Str += """" & txtlast.Text & """"
Str += " where IDNumber="
Str += txtfirst.Text.Trim()
cmd = New OleDbCommand(Str, con)
cmd.ExecuteNonQuery()
con.Close()
con.Open()
Str = "UPDATE studentsrecord set Course="
Str += """" & cbocourse.Text & """"
Str += " where IDNumber="
Str += txtIDNumber.Text.Trim()
cmd = New OleDbCommand(Str, con)
cmd.ExecuteNonQuery()
con.Close()
con.Open()
Str = "update studentsrecord set Password="
Str += """" & txtpassword.Text & """"
Str += " where IDNumber="
Str += txtIDNumber.Text.Trim()
cmd = New OleDbCommand(Str, con)
cmd.ExecuteNonQuery()
con.Close()
ds.Clear()
da = New OleDbDataAdapter("SELECT * FROM studentsrecord ORDER BY ID", con)
da.Fill(ds, "evaluation")
MsgBox("Updated Successfully...")
Catch ex As Exception
MsgBox(ex.Message & "," & ex.Source)
Finally
If con.State = ConnectionState.Open Then con.Close()
End Try

You don't have to issue a separate UPDATE statement for each field, you can update multiple fields in a single UPDATE statement. Also a better choice is to use parametrized query instead of concatenating strings.
Try this inside of your TRY/CATCH block:
Dim Str As String
Str = "update studentsrecord set FirstName = #FirstName, LastName = #LastName, Course = #Course, Password = #Password where IDNumber = #IDNumber "
cmd = New OleDbCommand(Str, con)
cmd.Parameters.AddWithValue("#FirstName", txtfirst.Text)
cmd.Parameters.AddWithValue("#LastName", txtlast.Text)
cmd.Parameters.AddWithValue("#Course", cbocourse.Text)
cmd.Parameters.AddWithValue("#Password", txtpassword.Text)
cmd.Parameters.AddWithValue("#IDNumber", txtIDNumber.Text.Trim())
con.Open()
cmd.ExecuteNonQuery()
ds.Clear()
da = New OleDbDataAdapter("SELECT * FROM studentsrecord ORDER BY ID", con)
da.Fill(ds, "evaluation")
MsgBox("Updated Successfully...")

Related

SQL Update not updating records in access

Trying to update the values of Comment and ProgressValue inside a table in access. The message box at the end pops up but none of the values are changed.
Sub UpdateWeeklyReport()
Dim con As OleDbConnection
Dim com As OleDbCommand
con = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\ProjectDatabase.mdb")
com = New OleDbCommand("Update WeeklyReport Set Comment = #Comment, ProgressValue = #ProgressValue Where [EntryDate]='" & CBDate.SelectedValue.ToString & "' AND [AdminNo]=" & CBAdmin.SelectedValue & " AND [ClassCode]='" & CBClass.SelectedItem.ToString & "'", con)
con.Open()
com.Parameters.Add("#Comment", OleDbType.LongVarChar).Value = txtComment.Text
com.Parameters.Add("#ProgressValue", OleDbType.Integer).Value = CBProgress.Text
com.ExecuteNonQuery()
con.Close()
MessageBox.Show("Report Changed")
intialconnection()
End Sub
End Class

Select data from different tables - vb.net

I'm trying to select some datas from my database and display it into the textboxes. First is i'm selecting the data from realestate.useraccounts and then to realestate.userprofiles. Its working on the useraccounts but not on the userprofiles.
`
Try
con.Open()
Dim q1 As String
q1 = "select userid,email from realestate.useraccounts where username='" & frmLogin.txtUsername.Text & "'"
cmd = New MySqlCommand(q1, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
lblUserid.Text = dr("userid")
txtEmail.Text = dr("email")
End If
con.Close()
con.Open()
Dim q2 As String
q2 = "select * from realestate.userprofiles where userid = '" & lblUserid.Text & "'"
cmd = New MySqlCommand(q2, con)
cmd.ExecuteNonQuery()
If dr.HasRows Then
dr.Read()
txtFirst.Text = dr("lastname")
txtLast.Text = dr("firstname")
txtAddress.Text = dr("address")
End If
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try`
You forgot dr = cmd.ExecuteReader() in the second one, so your dr.Read cant read on your second query.
You may also close your reader after use use it.
Let's try this:
Try
con.Open()
Dim q1 As String
q1 = "select userid,email from realestate.useraccounts where username='" & frmLogin.txtUsername.Text & "'"
cmd = New MySqlCommand(q1, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
lblUserid.Text = dr("userid")
txtEmail.Text = dr("email")
End If
dr.Close()
con.Close()
con.Open()
Dim q2 As String
q2 = "select * from realestate.userprofiles where userid = '" & lblUserid.Text & "'"
cmd = New MySqlCommand(q2, con)
cmd.ExecuteNonQuery()
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
txtFirst.Text = dr("lastname")
txtLast.Text = dr("firstname")
txtAddress.Text = dr("address")
End If
dr.Close()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try`

show multiple data on rcihtextbox from database column error

What's wrong with my code? the error happens on UPDATE block "
If reader1.Read() Then
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar")" Conversion from type 'DBNull' to type 'String' is not valid
Private Sub txtURThirdMolar_KeyDown(sender As Object, e As KeyEventArgs) Handles txtURThirdMolar.KeyDown
MySqlConn.open()
If e.KeyCode = Keys.Enter Then
query1 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd1 = New MySqlCommand(query1, MySqlConn)
reader = cmd1.ExecuteReader
If reader.HasRows Then
Dim i As Integer
With cmd
.Connection = MySqlConn
.CommandText = "UPDATE teethhistory SET Up_Right_3rd_Molar = concat('" & txtURThirdMolar.Text & Environment.NewLine & "',Up_Right_3rd_Molar) WHERE Patient_ID_Number = " & lblID.Text
reader.Close()
i = .ExecuteNonQuery
End With
If i > 0 Then
MsgBox("Updated!", MsgBoxStyle.Information, "Success")
txtURThirdMolar.Text = ""
Dim query2 As String
Dim reader1 As MySqlDataReader
Dim cmd2 As MySqlCommand
query2 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd2 = New MySqlCommand(query2, MySqlConn)
reader1 = cmd2.ExecuteReader
If reader1.Read() Then
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar") 'THIS IS WHERE THE ERROR OCCURS
End If
Else
MsgBox("Failed", MsgBoxStyle.Information, "Failed")
End If
Else
Dim cmd As MySqlCommand = MySqlConn.CreateCommand
cmd.CommandText = String.Format("INSERT INTO teethhistory (Patient_ID_Number, Fullname, Up_Right_3rd_Molar )" &
"VALUES ('{0}' ,'{1}' ,'{2}')",
lblID.Text,
lblFullname.Text,
txtURThirdMolar.Text)
reader.Close()
Dim affectedrows As Integer = cmd.ExecuteNonQuery()
If affectedrows > 0 Then
MsgBox("Saved!", MsgBoxStyle.Information, "Success")
txtURThirdMolar.Text = ""
Dim query2 As String
Dim reader2 As MySqlDataReader
Dim cmd2 As MySqlCommand
query2 = "SELECT * FROM teethhistory WHERE Patient_ID_Number ='" & lblID.Text & "'"
cmd2 = New MySqlCommand(query2, MySqlConn)
reader2 = cmd2.ExecuteReader
If reader2.Read() Then
rtbURThirdMolar.Text = reader2.Item("Up_Right_3rd_Molar")
End If
Else
MsgBox("Saving failed.", MsgBoxStyle.Critical, "Failed")
End If
End If
End If
MySqlConn.close()
End Sub
use
rtbURThirdMolar.Text = reader1.Item("Up_Right_3rd_Molar").ToString
this is because the item you are referencing is NULL.

Error in updating MS Access database with Visual Basic 2010

This code is not working. An error message pops up saying:
Syntax Error (missing operator) in query exprssion SubName = Gussing Game.
Here is my code please help. I need this code to update the Microsoft Access Database.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim Str As String
Con.Open()
Str = "update vb set AssignDate="
Str += """" & txtADate.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set DueDate="
Str += """" & txtDDate.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Weight="
Str += """" & txtWeight.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Reference="
Str += """" & txtReference.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Comment="
Str += """" & txtComment.Text & """"
Str += " where SubName ="
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Con.Open()
Str = "update vb set Statues="
Str += """" & txtStatues.Text & """"
Str += " where SubName ="
Str += txtAName.Text.Trim()
Cmd = New OleDbCommand(Str, Con)
Cmd.ExecuteNonQuery()
Con.Close()
Dst.Clear()
Dad = New OleDbDataAdapter("SELECT * FROM vb ORDER BY SubName", Con)
Dad.Fill(Dst, "assignment")
MessageBox.Show("Record Updated!", "Caution", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
MsgBox(ex.Message & "," & ex.Source)
End Try
Con.Close()
End Sub
You didn't quote the string scalar you compare SubName with:
Str = "update vb set AssignDate="
Str += """" & txtADate.Text & """"
Str += "where SubName = "
Str += txtAName.Text.Trim() ' should be """"&txtAName.Text.Trim()&""""
I'll skip over what an extremely bad way of querying this is.

VB.net 2010 dataset updates but access database remains same

I have tried to insert or update from my vb.net form into MS-Access database.
The dataset updates but the access database wont. Below is my code.
Try
Dim addLocation As String = "Insert into Provider (StateCode, Provider)" _
& "values ('" & ComboBox1.Text & "', '" & TextBox2.Text & "')"
Dim sqlcommand As New OleDbCommand
conn.Open()
With sqlcommand
.CommandText = addLocation
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("One record added", MsgBoxStyle.Information)
refreshGrid()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Private Sub refreshGrid()
cnString = "PROVIDER = Microsoft.ace.oledb.12.0;data source =" & Application.StartupPath & "\HCHPClosedIn.accdb"
sqlQRY = "SELECT * FROM Provider"
conn = New OleDbConnection(cnString)
Try
conn.Open()
da = New OleDbDataAdapter(sqlQRY, conn)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "Customers")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers"
End Try
End Sub
Its been a while but I think I recall Access is kinda picky with commit. Try this:
With sqlcommand
.CommandText = addLocation
.Connection = conn
.ExecuteNonQuery()
.transaction = trans
End With
Trans.Commit()