asp.net - c# entity , deleting a db record from a drop down list selected value -
how delete record drop down list selected value?
i have populated drop down list
database , having problems delete delete button. doing right way? ( sorry if codes messy )
this how populate drop down list:
protected void droptask() { droplisttask.datasource = daotask.getall(); droplisttask.datatextfield = "taskname"; droplisttask.datavaluefield = "taskid"; droplisttask.databind(); }
this delete button :
protected void btndelete_click(object sender, eventargs e) { if (!page.ispostback) { model.task del= new model.task(); del.taskid = convert.toint32(droplisttask.selectedvalue); daotask.delete(del); daotask.save(); } }
i need add postback btn codes, if removed it gives me error says;
the object cannot deleted because not found in objectstatemanager.
so put in postback , error gone. don't know if right way. please advise me how to correct way.
thanks in advance.
this: if (!page.ispostback)
means piece of code run on first run of page (so not when click button). button click go asp page post should invert if
, in btndelete_click
method know postback can skip if altogether.
i believe error went away because, indeed, not executing code in if
block, nothing deleted.
i don't know daotask
or how persistance in code way of deleting object db not sound right... creating new 1 , assigning different id flow should different:
first find existing object based on id, delete it:
// don't know if 'find' method need, should check based on // libraries using saving/loading db model.task del = daotask.find(convert.toint32(droplisttask.selectedvalue)); daotask.delete(del); daotask.save();
googling error got should have pointed in right direction fact problem db code, rather asp postback: the object cannot deleted because not found in objectstatemanager
update
based on comment should this:
model.task del = daotask.gettask(droplisttask.selecteditem.text); daotask.delete(del); daotask.save();
by way, add gettask(int id)
method daotask
class able find tasks id rather name.
Comments
Post a Comment