Reputation: 1
Imports System.Data.SqlClient
Public Class Form_sign_in_
Dim connection As New SqlConnection("Server=TAYYAB-PC\SQLEXPRESS;Database=Hospital Management;Integrated Security=True;Trusted_Connection=True;User=sa;Pwd=Password1;")
Dim SQL As New SQL_Control
Private Sub Form_sign_in__Load(sender As Object, e As EventArgs) Handles MyBase.Load
If SQL.HasConnection = True Then
MsgBox("Succesfully connected! ")
End If
End Sub
Public Sub Executequery(query As String)
Dim command As New SqlCommand(query, connection)
connection.Open()
> **command.ExecuteNonQuery()**// the problem part
connection.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Insertquery As String = "INSERT INTO Patient Information
(F_Name , L_Name, Patient ID, Disease, First Visit, Last Visit) VALUES ( ' " & TextBox2.Text & " ' , ' " & TextBox3.Text & " ' , ' " & TextBox4.Text & " ' , ' " & TextBox5.Text & " ' , ' " & TextBox6.Text & " ' , ' " & TextBox7.Text & " ' ,)"
Executequery(Insertquery)
MsgBox("Data Inserted")
End Sub
End Class
The problem is there:
System.Data.SqlClient.SqlException: Incorrect syntax near 'Information'.
Anyone knows the solution I am Stuck with a problem in my project
Upvotes: 0
Views: 62
Reputation: 11
In "Insertquery" you use a comma (,
) - remove that.
Try this
Dim cmd as new sqlcommand("INSERT INTO [Patient Information](F_Name ,L_Name, Patient ID, Disease, First Visit, Last Visit) VALUES ( ' " & TextBox2.Text & " ' , ' " & TextBox3.Text & " ' , ' " & TextBox4.Text & " ' , ' " & TextBox5.Text & " ' , ' " & TextBox6.Text & " ' , ' " & TextBox7.Text & " ' ,con")
for executing the the command connection is required.
in your program, remove the comma in the sqlcommand.
Upvotes: 0
Reputation: 37
Is your table name really “Patient Information” with a space?
If so try adding [ ] around it. Also, add these round “Patient ID”,”First Visit” and “Last Visit”
Failing that. Try running the query in SSMS and replace the textbox.texts with manually entered strings.
SSMS should tell you anything wrong with the query.
Upvotes: 1