user3625236
user3625236

Reputation: 61

C# Sql Column name is not valid

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

Upvotes: 1

Views: 973

Answers (2)

Raging Bull
Raging Bull

Reputation: 18767

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.

Upvotes: 1

Ramy M. Mousa
Ramy M. Mousa

Reputation: 5943

Try This :

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

Upvotes: 1

Related Questions