I open Database connection in my controller and pass the opened connection to my Model for using that connection.
but since 2nd time calling to the controller(Ajax), it return an error message that say 'Connection must be open.' I reloaded page, and call the controller again then it works well. but for the 2nd time calling, it keep return that error message.
My controller has the code to open DB Connection, so I believed, it should open database for every processing(Calling). and the (open) code is in using{} block so after using it, it should be closed automatically.
but I think I'm doing wrong :(
here is my code,
public JsonResult myControllerMethod() // Action Controller
{
using (AdsConnection conn = new AdsConnection(connString))
{
conn.Open();
AdsTransaction txn = conn.BeginTransaction(); // Begin Transaction
try
{
MyModel mo = new MyModel();
mo.doProcess(conn); // pass connection to model
txn.Commit();
return Json(new { success = true });
}
catch (Exception e)
{
txn.Rollback();
return Json(new { success = false, message = e.Message });
}
}
}
MyModel
public void doProcess(AdsConncection conn){ // Model
try{
..
//AdsCommand select, update, delete and insert....
..
}catch(Exception e){
throw e;
}
}
Anybody know, what I am doing wrong? please advice me.
Thanks