sql server - Simple select query not working with vb.net while delete query works fine -
i writing simple sql server query operation through vb.net application. having strange problems.
this line giving error:
dr = cmd.executereader()
this giving me error "invalid column name abhishek
." here abhishek
data providing in textbox1.text
. not able think of mistake side simple query. able run other queries, delete queries, on same table in different form, not database problem.
any clue what's going wrong?
reginfo
table name. name
1 of fields.
my complete code below:
imports system.data.sql imports system.data.sqlclient public class form9 dim con new sqlconnection() dim cmd new sqlcommand() private sub button1_click(sender system.object, e system.eventargs) handles button1.click cmd.commandtext = "select * reginfo name=" + (textbox1.text) + "" dim dr sqldatareader con.open() cmd.connection = con dr = cmd.executereader() '<<< line creating problem if dr.read() textbox2.text = dr(0).tostring() end if con.close() end sub private sub form8_load(sender system.object, e system.eventargs) handles mybase.load con.connectionstring = "data source=abhishek-pc\sqlexpress;initial catalog=locserver;integrated security=true;pooling=false" end sub private sub button2_click(sender system.object, e system.eventargs) handles button2.click end sub end class
if name field text field need enclose textbox in single quotes, bad advice give. approach kind of situations through parameterized query
cmd.commandtext = "select * reginfo name=@name" cmd.parameters.addwithvalue("@name", textbox1.text) dim dr sqldatareader con.open() cmd.connection = con dr = cmd.executereader()
also, not keep global objects connection or command. practice instantiate connection late possible , close possible, better inside using block
using con = new sqlconnection(...connection string here....) using cmd = new sqlcommand("select * reginfo name=@name", con) con.open() cmd.parameters.addwithvalue("@name", textbox1.text) using dr = cmd.executereader '.... reading end using end using end using
in way connection kept open minimum time possible and, in case of exceptions closed , disposed appropriately.
Comments
Post a Comment