Skip to content
Advertisement

C# invalid column name problem but is different from others

Hello i have a table called Card_tbl with a cell CardID. The problem is that when i insert only numbers like 12345 they are uploaded in the database, but when i use mix letters like Q1234R it will say INVALID COLUMN NAME Q1234R I tried many things to fix it but no success. THE CELL IS ON VARCHAR(50) this is my code

Con.Open();
            SqlCommand cmd = new SqlCommand("insert into Card_tbl values(" +txtCardNumber.Text+")", Con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Admin Successfully Added!");
            Con.Close();
            populate();
            //updateroomstate();
            Clean();

the error is shown here cmd.ExecuteNonQuery();

Thank you in advance.

Advertisement

Answer

Try this instead

Insert into Card_tbl(CardID) values(‘” +txtCardNumber.Text+”‘)”

https://www.w3schools.com/sql/sql_insert.asp

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement