ado.net - Can not found Row 0 in vb.NET -
i try select first row , first item sql but, show me message "can not found row 0 "
sqlcmd1 = "select type.name type inner join room on type.tid = room.typeid (((room.roid)= " + (roomcb.selectedindex + 1).tostring() + "));" ad = new oledb.oledbdataadapter(sqlcmd1, cnn) ds = new dataset ad.fill(ds) try typetxt.text = ds.tables(0).rows(0).item(0) catch ex exception msgbox(ex.message) end try
why filling datatable if want retrieve single field?
executescalar executes query, , returns first column of first row in result set returned query. additional columns or rows ignored.
also should use parameterized query in order avoid sql injection.
off top of head...
dim roomid string = roomcb.selectedindex + 1.tostring() dim sql string = "select type.name type inner join room on type.tid = @parameter" using cn new sqlconnection("your connection string here"), _ cmd new sqlcommand(sql, cn) cn.open() cmd.parameters.add("@parameter", sqldbtype.varchar, 50).value = roomid typetxt.text = cmd.executescalar().tostring() cn.close() end using
Comments
Post a Comment