1

I have a ASP.NET Web application project in which when ever user login , according to User logged in, we get have different connection string name from database and store that in session.

In Database access Layer

I have created a class with name ShareSession

public class ShareSession
{

    public string ConnectionString1 { get; set; }
    public string ConnectionString2 { get; set; }


    public ShareSession()
    {
        try
        {
            ConnectionString1 = Convert.ToString(HttpContext.Current.Session["ConnectionStringname1"]);
            ConnectionString2 = Convert.ToString(HttpContext.Current.Session["ConnectionStringname2"]);
        }
        catch (Exception)
        {
            HttpContext.Current.Response.Redirect("~/DashBoard/Login.aspx");
            throw;
        }
    }


}

While accessing this object i just create object of this class.

 ShareSession objShareSession = new ShareSession();

 var Connection_Database = Convert.ToString(objShareSession.ConnectionString1);



using (SqlConnection conn = new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings[Connection_Database].ConnectionString)))
{
// Database Logic
}

It is performing slow is there any other solution for it.

Scenario:- i am setting dynamic connection string name from database everytime when use login it has different region database for normalization if one login from india then his data will be inserted in indian database if he login from USA then his data is inserted into USA database , in this condition how can i send this dynamic name of connection to data access layer

Saineshwar Bageri - MVP
  • 3,851
  • 7
  • 41
  • 47
  • Have a read of https://stackoverflow.com/a/8422186/34092 – mjwills Jun 19 '17 at 10:34
  • 1
    What makes you think this is causing the slowness? Have you profiled your application to determine where it is being slow? – RB. Jun 19 '17 at 10:35
  • hi mjwills my question is not that simple understand it i am setting dynamic connection string name from database everytime when use login it has different region database for normalization if one login from india then his data will be inserted in indian database if he login from USA then his data is inserted into USA database , in this condition how can i send this dynamic name of connection to data access layer – Saineshwar Bageri - MVP Jun 19 '17 at 10:39
  • hi RB when ever i perform any CRUD operation this class object will be created to get connection string name right there are 60 classes in application which will access this logic. – Saineshwar Bageri - MVP Jun 19 '17 at 10:44
  • my opinion is pass only region name and in data layer user if else of switch for connection string – ishan joshi Jun 19 '17 at 10:53
  • I would be very surprised if the cost of creating ShareSession is your issue. The performance cost is far more likely to be related to the cost of creating database connections. – mjwills Jun 19 '17 at 11:06
  • hi ishan joshi but is there any issue in above code snippet may i know – Saineshwar Bageri - MVP Jun 19 '17 at 11:31
  • Hi mjwills is there any issue in above code snippet may i know which will help me – Saineshwar Bageri - MVP Jun 19 '17 at 11:42
  • When you profiled it, where was the slowness? https://stackoverflow.com/questions/3927/what-are-some-good-net-profilers – mjwills Jun 19 '17 at 11:45
  • User which are using application are telling is there any issue really in code – Saineshwar Bageri - MVP Jun 19 '17 at 11:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147075/discussion-between-saineshwar-and-mjwills). – Saineshwar Bageri - MVP Jun 19 '17 at 13:50

1 Answers1

0

Hi there Create a Sealed class for the User and save the connection string there since a sealed class can have only 1 instance(per user) there will not be issue of creating multiple instance to get the same connection string next i would suggest you to pass the connection string as string parameter from BLL to DAL instead of reading it in DAL (just read the connection string from web config to your constructor class of BLL or in DAL your wish )

RAHUL S R
  • 1,569
  • 1
  • 11
  • 20
  • but is it good to pass connection string name as a parameter in every method – Saineshwar Bageri - MVP Jun 20 '17 at 04:50
  • it dosen't matter just make sure you are passing it from BLL and not from Presentation Layer if you are that concern then you can initialize the connection string in the Constructor function of that particular DAL again its on you i dont think passing connection string from BLL to DAL gonna make a big compromise . – RAHUL S R Jun 20 '17 at 05:06