You have an error in your SQL syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnmar
    New Member
    • Dec 2011
    • 1

    You have an error in your SQL syntax

    Imports MySql.Data.MySq lClient
    Imports System.Data.Sql Client

    Public Class Form2
    Dim MysqlConn As MySqlConnection
    Dim sqlconnection As MySqlConnection = New MySqlConnection
    Dim serverstring As String = "server=localho st; user id=root; password=; database = project"

    Private Sub Save_Click(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles Save.Click
    Dim sqlstatement As String = "Insert into subject('ID','C ode','Descripti on','Unit','Hou rs') values('" & TextBox5.Text & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
    savenames(sqlst atement)
    End Sub

    Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s) Handles Me.Load


    sqlconnection.C onnectionString = serverstring

    Try
    If sqlconnection.S tate = ConnectionState .Closed Then
    sqlconnection.O pen()
    MsgBox("Success fully connected to mysql database")
    Else
    sqlconnection.C lose()
    MsgBox("Connect ion is closed.")
    End If
    Catch ex As Exception
    MsgBox(ex.ToStr ing)
    End Try
    End Sub
    Public Sub savenames(ByRef sqlstatement As String)

    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd

    .CommandText = sqlstatement
    .CommandType = CommandType.Tex t
    .Connection = SqlConnection
    .ExecuteNonQuer y()
    End With


    SqlConnection.C lose()

    MsgBox("Success fully Added!")
    SqlConnection.D ispose()
    End Sub

    End Class
    Attached Files
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You don't need quotes around field names.

    Comment

    • Ammu
      New Member
      • Aug 2011
      • 78

      #3
      Code:
      Dim sqlstatement As String = "Insert into subject(ID,Code,Description,Unit,Hours) values('" & TextBox5.Text & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"

      Comment

      Working...