Best Place to Declare SqlConnection and Data Access Functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123sajan
    New Member
    • Mar 2014
    • 1

    Best Place to Declare SqlConnection and Data Access Functions

    Dear Geeks,

    I got stuck in a problem in morning
    as till now I have a class CGeneral
    where I have declared my SqlConnection con as static and invoked there like this
    Code:
     private static string constr = DecValue(ConfigurationManager.ConnectionStrings["FinCon"].ConnectionString);
    public static SqlConnection con =new SqlConnection(constr);
    It was working well in single user web application but when we try to login with multiple users we stuck in a problem we get error.

    So what will be the best place to declare it and as i will need to open it again and again and close it again and again.

    So, I want to get rid of this issue also please suggest me as i am in a huge trouble right now.

    I am attaching a file which is my Class file from where I do the things for my application
    Attached Files
    Last edited by Frinavale; Mar 11 '14, 02:41 PM. Reason: Added Code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Do not use a static connection because crazy things can happen when it is accessed by multiple threads (users).

    Instead, create a connection, open it, use it, and close it in side the methods that use it.

    You can keep your connection string static if you want to though.

    -Frinny

    Comment

    Working...