sql server - How to properly connect to database in vb -
i need on codes and/or connection.i'm getting error says:
an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll
additional information: executereader requires open , available connection. connection's current state closed.
in code:(line 4)
cmd.connection = cn cmd.commandtext = "select id, lastname, firstname tblmembers id = @id" cmd.parameters.add(new sqlclient.sqlparameter("@id", sqldbtype.int)).value = dg1.item(0, e.rowindex).value dr = cmd.executereader if dr.hasrows() dr .read() txtlname.text = dr("lastname") txtfname.text = dr("firstname") end end if
but if try open connection. happens.
an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll
additional information: connection not closed. connection's current state open.
with code:(line 1)
cn.open() cmd.connection = cn cmd.commandtext = "select id, lastname, firstname tblmembers id = @id" cmd.parameters.add(new sqlclient.sqlparameter("@id", sqldbtype.int)).value = dg1.item(0, e.rowindex).value dr = cmd.executereader if dr.hasrows() dr .read() txtlname.text = dr("lastname") txtfname.text = dr("firstname") end end if
dim cn sqlconnection dim cmd sqlcommand dim dr sqldatareader dim connetionstring string dim process_id string dim processname string connetionstring = "data source=servername;initial catalog=databasename;user id=username;password=password" cn = new sqlconnection(connetionstring) cn.open() cmd = new sqlcommand("select process_id,processname dbo.process_master process_id = @id", cn) cmd.parameters.add(new sqlclient.sqlparameter("@id", sqldbtype.int)).value = "21" dr = cmd.executereader if dr.hasrows() dr.read() process_id = dr("process_id") processname = dr("processname") end end if
Comments
Post a Comment