Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

sql - System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'.' on Button Save

I don't know what's wrong with my code, when I add data then save, I get this error. FYI I use Visual Basic language.

[1][This is the error of my code] [1][https://i.stack.imgur.com/sxSie.png]

Private Sub TsSave_Click(sender As Object, e As EventArgs) Handles TsSave.Click
    If aksi = "insert" Then
        TBL_BukuTableAdapter.InsertQuery(txtJudulBuku.Text, txtPengarang.Text, txtPenerbit.Text, txtTahunTerbit.Text, txtkategori.Text)
        TsRefresh.Text = "Refresh"
    Else
        TBL_BukuTableAdapter.UpdateQuery(txtJudulBuku.Text, txtPengarang.Text, txtPenerbit.Text, txtTahunTerbit.Text, txtkategori.Text, txtIdbuku.Text)
    End If
    Me.TBL_BukuTableAdapter.Fill(Me.UASDataSet.TBL_Buku)

    TsAdd.Enabled = True
    TsDelete.Enabled = True
    TsSearch.Enabled = True
    aksi = "update"
End Sub

This is my full code

Public Class Form1
    Dim aksi As String = "update"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'UASDataSet.TBL_Buku' table. You can move, or remove it, as needed.
        Me.TBL_BukuTableAdapter.Fill(Me.UASDataSet.TBL_Buku)
        txtIdbuku.ReadOnly = True
    End Sub

    Private Sub TsAdd_Click(sender As Object, e As EventArgs) Handles TsAdd.Click
        TsAdd.Enabled = False
        TsDelete.Enabled = False
        TsSearch.Enabled = False
        TsRefresh.Text = "Cancel"
        aksi = "insert"
        TBLBukuBindingSource.AddNew()
        txtIdbuku.Text = TBL_BukuTableAdapter.IdBukuBaru()
        txtJudulBuku.Focus()
    End Sub

    Private Sub TsSave_Click(sender As Object, e As EventArgs) Handles TsSave.Click
        If aksi = "insert" Then
            TBL_BukuTableAdapter.InsertQuery(txtJudulBuku.Text, txtPengarang.Text, txtPenerbit.Text, txtTahunTerbit.Text, txtkategori.Text)
            TsRefresh.Text = "Refresh"
        Else
            TBL_BukuTableAdapter.UpdateQuery(txtJudulBuku.Text, txtPengarang.Text, txtPenerbit.Text, txtTahunTerbit.Text, txtkategori.Text, txtIdbuku.Text)
        End If
        Me.TBL_BukuTableAdapter.Fill(Me.UASDataSet.TBL_Buku)

        TsAdd.Enabled = True
        TsDelete.Enabled = True
        TsSearch.Enabled = True
        aksi = "update"
    End Sub

    Private Sub TsDelete_Click(sender As Object, e As EventArgs) Handles TsDelete.Click
        If MessageBox.Show("Delete Data?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            TBL_BukuTableAdapter.DeleteQuery(txtIdbuku.Text)
            Me.TBL_BukuTableAdapter.Fill(Me.UASDataSet.TBL_Buku)
        End If
    End Sub

    Private Sub TsRefresh_Click(sender As Object, e As EventArgs) Handles TsRefresh.Click
        TsRefresh.Text = "Refresh"
        Me.TBL_BukuTableAdapter.Fill(Me.UASDataSet.TBL_Buku)
    End Sub

    Private Sub TsSearch_Click(sender As Object, e As EventArgs) Handles TsSearch.Click
        If TxtKeyword.Text <> "" Then
            TBL_BukuTableAdapter.FillByJudulBuku(UASDataSet.TBL_Buku, TxtKeyword.Text)
        End If
    End Sub
End Class

Edit: I just add the Id_buku to this code. But it still don't work [this is the code][2] [2][https://i.stack.imgur.com/i3RRQ.png]

If aksi = "insert" Then TBL_BukuTableAdapter.InsertQuery(txtid_buku.text,txtJudulBuku.Text, txtPengarang.Text, txtPenerbit.Text, txtTahunTerbit.Text, txtkategori.Text)

question from:https://stackoverflow.com/questions/65849126/system-data-sqlclient-sqlexception-incorrect-syntax-near-on-button-save

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay guys , I just found the answer why error. I think the primary key can't set to identity increment when want to add some column... Is it correct? I just set the identity increment to NO, and finally I solve the problem... :)

Correct me if I'm wrong...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...