Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have buttons "UPDATE" and "DELETE". They have the same errors...


An expression of non-boolean type specified in a context where a condition is expected, near 'ID'.

What I have tried:

VB
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Try
            conn = New SqlConnection(strcon)
            conn.Open()
            Dim str As String = "Update PatientInfo set Surname=@Surname,Firstname=@Firstname,MI=@MI,Gender=@Gender,Birthday=@Birthday,Address=@Address,Contact=@Contact,Email=@Email Where Patient ID=@PID"
            cmd = New SqlCommand(str, conn)
            cmd.Parameters.AddWithValue("@Surname", txtSurname.Text)
            cmd.Parameters.AddWithValue("@Firstname", txtFirstname.Text)
            cmd.Parameters.AddWithValue("@MI", txtMI.Text)
            cmd.Parameters.AddWithValue("@Gender", CmbxGender.Text)
            cmd.Parameters.AddWithValue("@Birthday", dtpBday.Text)
            cmd.Parameters.AddWithValue("@Address", txtAdd.Text)
            cmd.Parameters.AddWithValue("@Contact", txtContact.Text)
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text)
            cmd.Parameters.AddWithValue("@PID", txtPID.Text)
            cmd.ExecuteNonQuery()
            MessageBox.Show("Data has been updated!")
            loadData()
            cmd.Dispose()
            conn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
'==========================================================================

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

        Dim cmd As New SqlCommand
        Try

            conn.ConnectionString = strcon
            conn.Open()
            cmd.Connection = conn
            cmd.CommandText = "Delete from PatientInfo Where Patient ID=" + txtPID.Text
            cmd.ExecuteNonQuery()
            MessageBox.Show("Data has been deleted")
            loadData()

            conn.Close()


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Posted
Updated 17-Jan-17 13:52pm
Comments
[no name] 17-Jan-17 18:41pm    
Try [Patient ID]=@PID

1 solution

It's a bad idea to have space characters in table and column names. In your SQL query you apparently have a column called "Patient ID". It should probably be "PatientID", but if you insist on keeping this column name with a space in it you MUST specify the column name in all SQL statements with square brackets:
DELETE FROM PatientInfo WHERE [Patient ID]=@PID"


Oh, and just because you only have one parameter in the DELETE query, that does NOT mean you can get away with string concatenating the Id on the end of the query. ALWAYS, ALWAYS, ALWAYS use parameterized queries, even if you're only using one parameter.
 
Share this answer
 
Comments
Member 12905345 17-Jan-17 21:14pm    
When I put the "DELETE FROM PatientInfo WHERE [Patient ID]=@PID"
An error occurs saying "Must declare the scalar variable "@PID"
and another error saying "Not allowed to change the "ConnectionString". The Connection's current state is open"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900