mysql - Edit form load. Error: "There is no row at position 0." [VB.net] -
im having problem on thesis. keeps showing message "there no row @ position 0.". though there data in mysql database. please help, i'm newbie. thanks!
private sub frmaccountedit_load(sender object, e eventargs) handles mybase.load txtuserid.text = frmaccountlist.accountcaller da = new mysqldataadapter("select * tbl_account userid = '" & txtuserid.text & "'", con) ds.reset() da.fill(ds) try cmbacctype.selectedindex = ds.tables(0).rows(0)(1).tostring txtusername.text = ds.tables(0).rows(0)(2).tostring txtpassword.text = ds.tables(0).rows(0)(3).tostring txtfirst.text = ds.tables(0).rows(0)(4).tostring txtlast.text = ds.tables(0).rows(0)(5).tostring catch ex exception messagebox.show(ex.message, "error!", messageboxbuttons.ok, messageboxicon.error) end try end sub
run mysql workbench , try query in there work. have more informations query. if there no row 0 query returns nothing. for row count before trying access position 0.
i included class used time. don't want hard code query textboxes that. attacked sql injection. use thing
sqlquery = "select * tbl_account userid=@coluid"
then use addparam function class add textboxes
dim xdb new mysql xdb.addparam("@coluid", txtuserid.text) xdb.execquery(sqlquery) if xdb.rows.count>0 'you have record imports mysql.data.mysqlclient public class mysql 'connection string mysql public sqlsource string = "server=1.1.1.1;userid=xuser;password=xpassword;" 'database connection classes private dbcon new mysqlconnection private sqlcmd mysqlcommand public dbda new mysqldataadapter public dbdt new datatable ' parameters public params new list(of mysqlparameter) ' stats public recordcount integer public exception string sub execquery(sqlquery string) dbcon.connectionstring = sqlsource try dbcon.open() sqlcmd = new mysqlcommand(sqlquery, dbcon) 'loads params query params.foreach(sub(p) sqlcmd.parameters.addwithvalue(p.parametername, p.value)) 'or 'for each p mysqlparameter in params ' sqlcmd.parameters.addwithvalue(p.parametername, p.value) ' next ' clears params params.clear() dbda.selectcommand = sqlcmd dbda.update(dbdt) dbda.fill(dbdt) dbcon.close() catch ex mysqlexception exception = ex.message dbcon.dispose() end try end sub ' add parameters list public sub addparam(name string, value object) dim newparam new mysqlparameter(name, value) params.add(newparam) end sub end class
Comments
Post a Comment