Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:49:52+00:00 2026-06-11T21:49:52+00:00

I open Database connection in my controller and pass the opened connection to my

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:49:53+00:00Added an answer on June 11, 2026 at 9:49 pm

    You shouldn’t pass a database connection to your view – your view should accept an object (as a model) that contains “solid” data. A view should have no logic of its own, and a Model’s logic should only be concerned with modifying internal state, such as performing validation (not verification) or some internal data transformation. The task of populating a Model from a database should be done by the controller or a separate mapping class (or by some kind of “Populate” method of the model, but this method should be called and contained within the controller’s action method).

    That said, the problem you’re experiencing is because you’re using using() blocks. A using() block will always call the IDisposable.Dispose() method of its subject object, in this case a DB connection. SqlConnection.Dispose calls its .Close method, which is why the connection object is closed whenever the method returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I open the database file and obtain a database connection using open() method of
How can I open a database connection in user class , where I can
If a MySQL database connection is already open from a script (say PHP ),
In c#.net when you open a connection to a database, in my case it's
In PhoneGap how do I create/open a database in Android? I am using this
I have a working controller for another stored procedure in the database, but I
Is it efficient method to open database connection for every database operation and accordingly
When you open a MySQL connection with mysql_connect(), it returns a link identifier. But
I'm trying to reuse an existing database connection so that I can do multiple
The application I'm working on has a single class that maintains a database connection.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.