Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir,

i am trying add the records to database, but after click on add button system gives "Syntax error" for cmd.ExecuteNonQuery().

plz guide / help

the coding part as follows:-

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If txtregno.Text = "" Then
            MsgBox("Please Enter Registration Id", MsgBoxStyle.Critical, "Registration Id is Required...!!!")
        End If
        cn.Open()
        qry = "INSERT into registration(RegNo,RegDt,Std,Div,SALUTION,SNAME,Gender,DOB,StdAdd,FatName,FatMobNo,PrvSchName,SchEnterDt,Fees,Status )values(" & txtregno.Text & ", " & DTP1.Value & ", " & cmbClass.Text & ", " & cmbDiv.Text & ", " & cmbSal.Text & ", " & txtSname.Text & ",'" & gender & "'," & DOBDTP.Value & "," & txtadd.Text & "," & txtFname.Text & "," & txtMobno.Text & "," & txtPrvSchName.Text & "," & ADMDTP.Value & "," & txtAdmFee.Text & "," & cmbStatus.Text & " )"

        qry1 = "Select * from Registration"
        cmd1 = New OleDbCommand(qry1, cn)

        dr1 = cmd1.ExecuteReader()

        If dr1.Read() = True Then

            cmd = New OleDbCommand(qry, cn)
            cmd.ExecuteNonQuery()
            dr1.Close()
            MsgBox("Record Saved Successfully.", MsgBoxStyle.Information, "NES Automation...!!!")
            'ds.Clear()
            qry = "Select * from Registration where fees = '0' and std = " & cmbClass.Text & " )"
            adp = New OleDbDataAdapter(qry, cn)
            adp.Fill(ds, "Registration")
            Grid.DataSource = ds
            Grid.DataMember = ds.Tables(0).ToString
            cn.Close()
        Else
            MsgBox("Record can not Save.", MsgBoxStyle.Critical, "NES Automation...!!!")
        End If
        cn.Close()
    End Sub


What I have tried:

i hv tried a lot to check what went wrong., but i didn't get the solution.
Posted
Updated 16-Jul-20 1:01am
Comments
Andre Oosthuizen 16-Jul-20 9:40am    
gender seems to be the problem, where is this variable declared?

VB
qry = "INSERT into registration(RegNo,RegDt,Std,Div,SALUTION,SNAME,Gender,DOB,StdAdd,FatName,FatMobNo,PrvSchName,SchEnterDt,Fees,Status )values(" & txtregno.Text & ", " & DTP1.Value & ", " & cmbClass.Text & ", " & cmbDiv.Text & ", " & cmbSal.Text & ", " & txtSname.Text & ",'" & gender & "'," & DOBDTP.Value & "," & txtadd.Text & "," & txtFname.Text & "," & txtMobno.Text & "," & txtPrvSchName.Text & "," & ADMDTP.Value & "," & txtAdmFee.Text & "," & cmbStatus.Text & " )"

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

Secondary problem, it is impossible to know what is exactly your query because it depend on the values of the parameters.
Only the debugger can show what is the real query.
 
Share this answer
 
Fixing your SQL Injection vulnerabilities will almost certainly fix your error:
VB.NET
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    If txtregno.Text = "" Then
        MsgBox("Please Enter Registration Id", MsgBoxStyle.Critical, "Registration Id is Required...!!!")
        Return
    End If
    
    Const query As String = "INSERT INTO registration (RegNo, RegDt, Std, Div, SALUTION, SNAME, Gender, DOB, StdAdd, FatName, FatMobNo, PrvSchName, SchEnterDt, Fees, Status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
    
    Const query2 As String = "SELECT * FROM Registration WHERE fees = '0' AND std = ?"
    
    Using connection As New OleDbConnection("...")
        connection.Open()
        
        Using command As New OleDbCommand(query, connection)
            command.Parameters.AddWithValue("@RegNo", txtregno.Text)
            command.Parameters.AddWithValue("@RegDt", DTP1.Value)
            command.Parameters.AddWithValue("@Std", cmbClass.Text)
            command.Parameters.AddWithValue("@Div", cmbDiv.Text)
            command.Parameters.AddWithValue("@Salutation", cmbSal.Text)
            command.Parameters.AddWithValue("@SName", txtSname.Text)
            command.Parameters.AddWithValue("@Gender", gender)
            command.Parameters.AddWithValue("@DOB", DOBDTP.Value)
            command.Parameters.AddWithValue("@StdAdd", txtadd.Text)
            command.Parameters.AddWithValue("@FatName", txtFname.Text)
            command.Parameters.AddWithValue("@FatMobNo", txtMobno.Text)
            command.Parameters.AddWithValue("@PrvSchName", txtPrvSchName.Text)
            command.Parameters.AddWithValue("@SchEnterDt", ADMDTP.Value)
            command.Parameters.AddWithValue("@Fees", txtAdmFee.Text)
            command.Parameters.AddWithValue("@Status", cmbStatus.Text)
            
            command.ExecuteNonQuery()
            
            MsgBox("Record Saved Successfully.", MsgBoxStyle.Information, "NES Automation...!!!")
        End Using
        
        Using command As New OleDbommand(query2, connection)
            command.Parameters.AddWithValue("@Std", cmbClass.Text)
            
            Dim da As New DataAdapter(command)
            Dim ds As New DataSet()
            da.Fill(ds, "Registration")
            
            Grid.DataSource = ds
            Grid.DataMember = ds.Tables(0).ToString()
        End Using
    End Using
End Sub
 
Share this answer
 
Comments
Andre Oosthuizen 16-Jul-20 9:36am    
The OP is using VB6, above code will not work. Not sure why people still insist in using such old dinosaurs. SQL Injection was still something someone thought about at the time and was not in implementation as yet. :)
Richard Deeming 16-Jul-20 9:44am    
EventArgs and OleDbCommand were not available in VB6. The OP is using VB.NET. :)
Andre Oosthuizen 16-Jul-20 9:58am    
You are (as always) totally correct, I do apologise for being half blind, must be the age...

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