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

1
  • C# variables do not magically get into an SQL query, even if mentioned in there in brackets. See e.g. stackoverflow.com/a/8218932/11683 Commented May 11, 2014 at 11:43

2 Answers 2

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.

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

8 Comments

Done as suggested above now it understands value of s1 but still it shows that it is column name.
Ok some part works it inserts value to Klientas column but the value is Klientas but textbox1.Text is not Klientas. set Klientas="'+s"' Something must be done with this part .
@GSerg: I am not a big fan of SQL injection, myself. Just trying to help OP to get what he is after. Anyway, Updated my answer.
Kliento ID is changed to Klientas read in description near Image.
@RagingBull you said you edited answer I don't see anything edited maybe you forgot to edit it?
|
1

Try This :

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

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.