2

I tried to update some data in C# I got

"Incorrect syntax near ')'." in line "da.UpdateCommand.ExecuteNonQuery();".

I browsed code a lot of time, and I can't see error anymore. So in my opinion should be OK, but isn't. Could you look at below code?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace klinika
{
    public partial class frModWla : Form
    {
        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS; Initial Catalog=Klinika; Integrated security=TRUE");
        SqlDataAdapter da = new SqlDataAdapter();
        BindingSource bs = new BindingSource();

        public frModWla()
        {
            InitializeComponent();
        }

        private void btnWyswietl_Click(object sender, EventArgs e)
        {
            bindingClear();
            da.SelectCommand = new SqlCommand("SELECT * FROM tbWlasciciel", cs);
            ds.Clear();
            da.Fill(ds);
            dg.DataSource = ds.Tables[0];

            bs.DataSource = ds.Tables[0];

            tbxImie.DataBindings.Add(new Binding("Text", bs, "Imie"));
            tbxNazwisko.DataBindings.Add(new Binding("Text", bs, "Nazwisko"));
            tbxMiejscowosc.DataBindings.Add(new Binding("Text", bs, "Miejscowosc"));
            tbxKodPocztowy.DataBindings.Add(new Binding("Text", bs, "Kod_pocztowy"));
            tbxAdres.DataBindings.Add(new Binding("Text", bs, "Adres"));
            tbxTelefon.DataBindings.Add(new Binding("Text", bs, "Telefon"));
            tbxEmail.DataBindings.Add(new Binding("Text", bs, "Email"));

            rekord();

        }

        private void bindingClear()
        {
            tbxImie.DataBindings.Clear();
            tbxNazwisko.DataBindings.Clear();
            tbxMiejscowosc.DataBindings.Clear();
            tbxKodPocztowy.DataBindings.Clear();
            tbxAdres.DataBindings.Clear();
            tbxTelefon.DataBindings.Clear();
            tbxEmail.DataBindings.Clear();
        }

        private void btnPoprzedni_Click(object sender, EventArgs e)
        {
            bs.MovePrevious();
            dgUpdate();
            rekord();
        }

        private void btnNastepny_Click(object sender, EventArgs e)
        {
            bs.MoveNext();
            dgUpdate();
            rekord();
        }

        private void btnPierwszy_Click(object sender, EventArgs e)
        {
            bs.MoveFirst();
            dgUpdate();
            rekord();
        }

        private void btnOstatni_Click(object sender, EventArgs e)
        {
            bs.MoveLast();
            dgUpdate();
            rekord();
        }

        private void dgUpdate()
        {
            dg.ClearSelection();
            dg.Rows[bs.Position].Selected = true;
            rekord();
        }

        private void rekord()
        {
            lblRecords.Text = "Rekord " + bs.Position + " z " + (bs.Count - 1);
        }

        private void btnUaktualnij_Click(object sender, EventArgs e)
        {
            da.UpdateCommand = new SqlCommand("UPDATE tbWlasciciel SET NAZWISKO = @NAZWISKO, IMIE = @IMIE, MIEJSCOWOSC = @MIEJSCOWOSC, KOD_POCZTOWY = @KOD_POCZTOWY, ADRES = @ADRES, TELEFON = @TELEFON, EMAIL = @EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA)", cs);
            da.UpdateCommand.Parameters.Add("@IMIE", SqlDbType.VarChar).Value = tbxImie.Text;
            da.UpdateCommand.Parameters.Add("@NAZWISKO", SqlDbType.VarChar).Value = tbxNazwisko.Text;
            da.UpdateCommand.Parameters.Add("@MIEJSCOWOSC", SqlDbType.VarChar).Value = tbxMiejscowosc.Text;
            da.UpdateCommand.Parameters.Add("@KOD_POCZTOWY", SqlDbType.VarChar).Value = tbxKodPocztowy.Text;
            da.UpdateCommand.Parameters.Add("@ADRES", SqlDbType.VarChar).Value = tbxAdres.Text;
            da.UpdateCommand.Parameters.Add("@TELEFON", SqlDbType.VarChar).Value = tbxTelefon.Text;
            da.UpdateCommand.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value = tbxEmail.Text;
            da.UpdateCommand.Parameters.Add("@ID_WLASCICIELA", SqlDbType.Int).Value = ds.Tables[0].Rows[bs.Position][0];
            cs.Open();
            da.UpdateCommand.ExecuteNonQuery();
            MessageBox.Show("Dane w bazie danych zostały zaktualizowane!", "Aktualizacja danych");
            cs.Close();
        }
    }
}
3
  • 2
    remove ")" at the end of your SqlCommand Commented Feb 21, 2013 at 20:23
  • @KyleC I suggest you write that as an answer. Commented Feb 21, 2013 at 20:25
  • Check at the end "WHERE ID_WLASCICIELA = @ID_WLASCICIELA)".........remove closing brace Commented Feb 22, 2013 at 4:37

3 Answers 3

4

You need to remove ")" at the end of your SqlCommand. You have no starting ( so it's telling you that the ) at the end is invalid syntax, which it is.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1869274 now you should "accept" the answer by clicking that empty check-mark on the left.
0

It looks like an extra ")" in your WHERE clause:

...@EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA)", cs)

where it should be:

...@EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA", cs)

Comments

0

Just remove ) in your SqlCommand. It is right of the @ID_WLASCICIELA parameter. You opened brackets once but you closed brackets twice.

Use this:

da.UpdateCommand = new SqlCommand("UPDATE tbWlasciciel SET NAZWISKO = @NAZWISKO, IMIE = @IMIE, MIEJSCOWOSC = @MIEJSCOWOSC, KOD_POCZTOWY = @KOD_POCZTOWY, ADRES = @ADRES, TELEFON = @TELEFON, EMAIL = @EMAIL WHERE ID_WLASCICIELA = @ID_WLASCICIELA", cs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.