0
private void button4_Click_1(object sender, EventArgs e)
    {
        string s = textBox1.Text;
        SqlCeConnection conn = new SqlCeConnection(@"Data Source=D:\Desktop\DB2\DB2\Database1.sdf");
        try
        {
            conn.Open();

            SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set Kliento ID = Kliento ID + s Where Kliento ID = 0 ", conn);
            cmd.ExecuteNonQuery();
            toolStripStatusLabel1.Text = "Duomenys įrašyti";
            conn.Close();
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }

Datatable schema shows that column exists but I still get error that Column name invalid maybe I forgot to write something please help me :) What I am trying to do is take text from textbox1.Text and update it datatable values where coulmn name is Kliento ID and value is 0.

https://i.stack.imgur.com/ZM4LN.jpg enter image description here

user3625236
  • 61
  • 1
  • 9

2 Answers2

1
  1. For MySQL: (since the original question tagged mysql)

    Use backticks(`) around the field name.

    Try this way: SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set Kliento ID = Kliento ID + "+s+" Where Kliento ID = 0 ", conn);

    Side note: Back ticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set it is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.

    Back ticks are necessary for situations like the following:

    SELECT id, `my name`, `another field` , `field,with,comma`
    
  2. For SQL Server: (since the image shows sql server)

    Try [] instead:

    SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set [Kliento ID] = [Kliento ID] + "+s+" Where [Kliento ID] = 0 ", conn);
    
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
1

Try this:

SqlCeCommand cmd = new SqlCeCommand("update Kambariai set [Kliento ID] = [Kliento ID] + " + s + " Where [Kliento ID] = 0", conn);
Tarik
  • 10,810
  • 2
  • 26
  • 40