Not updating

Pgill

Member
Joined
Nov 26, 2019
Messages
10
Programming Experience
Beginner
The following is not updating when I transfer to sql. I keep getting this error. I am new to this and am unable to figure where my error is . and I am getting a System.Data.SqlException:'incorrect syntax near ','.'

The truck VarChar is what is not updating
 
Last edited by a moderator:
I'd ask you what line the error is on, but since you can't follow simple instructions, you are simply now denying yourself support since I have asked you before to post your code in code tags.
This is also the second time I've asked you to post your code in code tags

Your code is not in code tags, and if it was I would be able to ask you for a line number for which the error appears. I also refuse to read that code dump until you post your code within the code tags provided by the forum :
[CODE=csharp] Your code here [/CODE]

Also post the exact error you are receiving word for word. I'm not one for assuming error or partial error messages, so posting exactly what the error is and on what line of code would be useful.
 
I'd ask you what line the error is on, but since you can't follow simple instructions, you are simply now denying yourself support since I have asked you before to post your code in code tags.


Your code is not in code tags, and if it was I would be able to ask you for a line number for which the error appears. I also refuse to read that code dump until you post your code within the code tags provided by the forum :
[CODE=csharp] Your code here [/CODE]

Also post the exact error you are receiving word for word. I'm not one for assuming error or partial error messages, so posting exactly what the error is and on what line of code would be useful.
I would be happy to do that but I can not figure out how to add it in that format
 
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlConnection cs = new SqlConnection("Data Source=newdell2017; Initial Catalog=Pocono20; Integrated Security=TRUE");
        /*cs.Open();
        MessageBox.Show(cs.State.ToString());
        cs.Close();*/
        SqlDataAdapter da = new SqlDataAdapter();
        BindingSource tblNamesBS = new BindingSource();


        public Form1()
         {
            InitializeComponent();
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {

            da.InsertCommand = new SqlCommand("INSERT into tblContacts VALUES(@FIRSTNAME, @LASTNAME, @Truck)", cs);
            da.InsertCommand.Parameters.Add("FIRSTNAME", SqlDbType.VarChar).Value = txtFirstName.Text;
            da.InsertCommand.Parameters.Add("LASTNAME", SqlDbType.VarChar).Value = txtLastName.Text;
            da.InsertCommand.Parameters.Add("Truck", SqlDbType.VarChar).Value = txtTruck.Text;

            //da.InsertCommand.Parameters.Add("StartDate", SqlDbType.Date).Value = Start_Date.Text;

            cs.Open();
            da.InsertCommand.ExecuteNonQuery();
            cs.Close();

        }

        private void BtnDisplay_Click(object sender, EventArgs e)
        {
            da.SelectCommand = new SqlCommand("Select * From TblContacts", cs);

            ds.Clear();

            da.Fill(ds);

            dg.DataSource = ds.Tables[0];

            tblNamesBS.DataSource = ds.Tables[0];

            txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
            txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LastName"));
            txtTruck.DataBindings.Add(new Binding("Text", tblNamesBS, "Truck"));
            records();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveNext();
            dgUpdate();
            records();
        }

        private void BtnPrevious_Click(object sender, EventArgs e)
        {
            tblNamesBS.MovePrevious();
            dgUpdate();
            records();
        }

        /*private void button2_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveLast();
            dgUpdate();
            records();
        }*/

        private void BtnFirst_Click(object sender, EventArgs e)
        {
            tblNamesBS.MoveFirst();
            dgUpdate();
            records();
        }
        private void dgUpdate()
        {
            dg.ClearSelection();
            dg.Rows[tblNamesBS.Position].Selected = true;
            records();
        }
       private void records()
        {
            label3.Text = "Records " + tblNamesBS.Position + " of " + (tblNamesBS.Count - 1);
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            da.UpdateCommand = new SqlCommand("UPDATE tblContacts SET FIRSTNAME =@FIRSTNAME, LASTNAME =@LASTNAME WHERE ID =@ID, Truck =@Truck", cs);
            da.UpdateCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = txtFirstName.Text;
            da.UpdateCommand.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = txtLastName.Text;
            da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];
            //da.UpdateCommand.Parameters.Add("@Truck", SqlDbType.VarChar).Value = txtTruck.Text;

            cs.Open();
            int x = da.UpdateCommand.ExecuteNonQuery();
            cs.Close();

            if (x >= 1)
                MessageBox.Show("Record has been updated");
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr;

            dr = MessageBox.Show("Are You Sure?\nThere is NO Undo once data has been DELETED", "CONFIRM Deletion", MessageBoxButtons.YesNo);

            if (dr == DialogResult.Yes)
            {
                da.DeleteCommand = new SqlCommand("DELETE FROM tblContacts WHERE ID = @ID", cs);
                da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];

                cs.Open();
                da.DeleteCommand.ExecuteNonQuery();
                cs.Close();

                ds.Clear();

                da.Fill(ds);
            }
            else
            {
                MessageBox.Show("Deletion Canceled");
            }

            
        }

        private void txtLastName_TextChanged(object sender, EventArgs e)
        {

        }

        //private void btnChangeForm_Click(object sender, EventArgs e)
        //{
        // Form2 f2 = new Form2();
        //this.Hide();
        //f2.ShowDialog();

        //}


    }
}
 
pocono snip prog.PNG
this the error that I get when I run the debug
 
The WHERE clause here is not valid:
C#:
UPDATE tblContacts SET FIRSTNAME =@FIRSTNAME, LASTNAME =@LASTNAME WHERE ID =@ID, Truck =@Truck
Here it is better formatted:
C#:
UPDATE tblContacts
    SET
        FIRSTNAME =@FIRSTNAME,
        LASTNAME =@LASTNAME
    WHERE
        ID =@ID,
        Truck =@Truck

See the comma on line 6? In WHERE clauses, the conditions are strung together using AND on OR keywords, not commas.
 
Back
Top Bottom