Error in updating MS Access database with Visual Basic 2010 - vb.net

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.

Related

vb.net msaccess update, may i ask whats wrong, why it does not update my data base?

I don't know what seems to be the problem,
can anyone help?
I already installed all necessary thing to run the program.
Public Sub main1_Click(sender As Object, e As EventArgs) Handles main1.Click
scorestanding.Text = standingscore
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "Database\user.mdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
myConnection.Open()
Dim str As String
str = "UPDATE login set[scorestand] = '" & scorestanding.Text & "' Where [username] = '" & Label1.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Try
cmd.CommandText = "UPDATE login set[scorestand] = '" & scorestanding.Text & "' Where [username] = '" & Label1.Text
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.Close()
main.Show()
End Sub
str = "UPDATE login set[scorestand] = '" & scorestanding.Text & "' Where [username] = '" & Label1.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Try
cmd.CommandText = "UPDATE login set[scorestand] = '" & scorestanding.Text & "' Where [username] = '" & Label1.Text
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.Close()
main.Show()
End Sub
It does nt have any eror but it also does nothing.

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.

I Cannot Update my database

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

Filling values in DataGridView

I want to display result of select query in a DataGridView with where clause in query. The condition is specified in a label's value. i.e
select *
from hospital_details
where location_name= '" & Label6.Text & "'"
Following is the code
Public Sub fill()
Try
createConnection()
da = New SqlDataAdapter
Dim c As String
c = Label6.Text
comm.CommandText = "select * from hospital_details where location_name= '" & Label6.Text & "'"
da.SelectCommand = comm
da.Fill(ds, "hospital_details")
DataGridView2.DataSource = ds.Tables(0)
DataGridView2.DataMember = "hospital_details"
DataGridView2.ReadOnly = True
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
End Try
End Sub
But its showing error : NullException
You can load data from sql server , follow the link
http://mssqlguid.blogspot.in/2013/07/how-to-load-data-into-datagridview-from.html

Data Reader formatting output

I'm using the following function to generate a list of users connected to a selected database. How would I change this to a single line for multiple identical results?
For example: "sa (3) - MYCOMPUTER" rather than listing "sa - MYCOMPUTER" three times?
Function ConnectedUsers(ByVal SelectedDatabase As String, ByVal SelectedInstance As String)
Dim myCommand As SqlCommand
Dim dr As SqlDataReader
Dim mystring As String = String.Empty
Try
Dim myConn As New SqlConnection(ConnectionString)
myConn.Open()
myCommand = New SqlCommand("select loginame,hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & ";", myConn)
dr = myCommand.ExecuteReader()
While dr.Read()
mystring += GetFullName(dr(0).ToString().Trim()) & " - " & dr(1).Trim() & vbCrLf
End While
dr.Close()
myConn.Close()
Catch e As Exception
MessageBox.Show(e.Message)
End Try
Return mystring
End Function
Thanks.
The SQL Command should be
select loginame, count(*) as Nbr, hostname from sysprocesses where db_name(dbid) = '" & SelectedDatabase & "' group by loginame;"
and you should change the display to show the count (Nbr in this example) to be something like:
mystring += GetFullName(dr(0).ToString().Trim()) & "(" & dr(1).Trim() & ") - " & dr(2).Trim() & vbCrLf