1
private void button4_Click_1(object sender, EventArgs e)
    {
        string s = textBox1.Text;

        string s1 = comboBox1.Text;
        string s2 = comboBox2.Text;
        SqlCeConnection conn = new SqlCeConnection(@"Data Source=D:\Desktop\DB2\DB2\Database1.sdf");
        try
        {
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set Klientas=[s]  Where [Kambario rūšis]=[s1]  ", conn);
            cmd.ExecuteNonQuery();
            toolStripStatusLabel1.Text = "Duomenys įrašyti";
            conn.Close();
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }

Image

I am trying to update my datatable by updating Klientas value with textbox1.Text which is made to string = s. It should work fine as Sql But I get an error saying that The column name is not valid Column = s1. s1 shouldn't be targeted as column name it should be used as column row value.

This is outdated image Kliento ID is changed to Klientas

Community
  • 1
  • 1
user3625236
  • 61
  • 1
  • 9
  • C# variables do not magically get into an SQL query, even if mentioned in there in brackets. See e.g. http://stackoverflow.com/a/8218932/11683 – GSerg May 11 '14 at 11:43

2 Answers2

1

Try this:

SqlCeCommand cmd = new SqlCeCommand("update Kambariai set Klientas="+s+"  Where [Kambario rūšis]='"+s1+"' ", conn);

Analysis:

From what you have tried, cmd has value like :

update Kambariai set Klientas=s Where [Kambario rūšis]=s1

From by putting proper double and single quotes around it, the value would be like:

update Kambariai set Klientas=1 Where [Kambario rūšis]='bar'

Side Note:

I would not recommend this method since it increases the risk of SQL injection. Use parameterized query instead.

Raging Bull
  • 18,113
  • 13
  • 47
  • 53
1

Try This :

     SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set Klientas='" + s +"'  Where [Kambario rūšis]='" + s1 + "'", conn);
Ramy M. Mousa
  • 5,301
  • 3
  • 37
  • 43