c# - How to solve this "error in your SQL syntax" -
i have connected database windows form, put values of database table column @ listbox, , want following: when select item listbox, column of table appear in textbox. more specific, drink names appear @ listbox( espresso,water etc) , want price appear @ textbox , when selected listbox. used following code that:
private void listbox1_selectedindexchanged(object sender, eventargs e) { string constring = "datasource=localhost;port=3306;username=root;password=root;"; string query = "select * apps.drinks drink_name ='" + listbox1.text + "'; "; mysqlconnection condatabase = new mysqlconnection(constring); mysqlcommand cmddatabase = new mysqlcommand(query, condatabase); mysqldatareader myreader; condatabase.open(); myreader = cmddatabase.executereader(); while (myreader.read()) { string dprice = myreader.getstring("drink_price"); pricebox.text = dprice; } }
after debug project, shows items @ listbox, when select them error "you have error in sql syntax; check manual corresponds mysql server version right syntax use near '='espresso'' @ line 1"
the code database following:
drop table if exists `apps`.`drinks`; create table `apps`.`drinks` ( `drink_name` varchar(45) not null, `drink_price` varchar(45) not null, primary key (`drink_name`) ) engine=innodb default charset=utf8; insert apps.drinks (drink_name,drink_price) values ('nes','1'),('espresso','1'), (...)
please can me??
the query fails on is:
"select * apps.drinks drink_name ='" + listbox1.text + "'; "
there have is =
incorrect, remove is
query looks like:
"select * apps.drinks drink_name ='" + listbox1.text + "'; "
also take comment of w0lf , use prepared statements prevent sql injection.