Sql transaction c# - exits try when ExecuteNonQuery() is run -
i'm trying implement sql transaction, however, i've run bit of bother. have simple database has 3 tables. users, post , comments. wish implement delete button delete user users table. initial problem had needed remove users fk both post , comment table, remove user user table. looked online , had suggested using transaction multiple server calls. here code:
public void deleteuser(int userid) { using (sqlconnection con = new sqlconnection(connectionstring)) { con.open(); sqltransaction sqltran = con.begintransaction(); try { using (sqlcommand command = new sqlcommand("delete comment userid = " + @userid, con)) { sqlparameter userid = new sqlparameter("@userid", sqldbtype.int); userid.value = userid; command.executenonquery(); } using (sqlcommand command = new sqlcommand("delete post userid = " + @userid, con)) { sqlparameter userid = new sqlparameter("@userid", sqldbtype.int); userid.value = userid; command.executenonquery(); } using (sqlcommand command = new sqlcommand("delete users userid = " + @userid, con)) { sqlparameter userid = new sqlparameter("@userid", sqldbtype.int); userid.value = userid; command.executenonquery(); } sqltran.commit(); } catch { sqltran.rollback(); } } }
the problem have when deleteuser method run, program gets far command.executynonquery in first using block, jumps catch. rolling changes. there isn't error codes displayed, i'm not sure going wrong.
any in matter appreciated.
you're missing command.transaction = sqltran;
.
do not use try {} catch {}
. use @ least try {} catch (exception ex) {}
-> give enough information solve problem on own.
Comments
Post a Comment