1

In the login page I am determining if the user is admin or not by the following code:

if (r.IsUserInRole(txtUserUsername.Text, "User") == true)
{
    connection = ConfigurationManager.ConnectionStrings["GameHutDBEntities1"].ToString();

    switch (new BusinessLayer.Users().ValidateLogin(txtUserUsername.Text, txtUserPassword.Text, connection))
    {
        case Helpers.LoginStatus.LoginSuccessful:
        {
            Response.Write("<Script> alert('Welcome to GameHut Admin Panel!')</Script>");
            FormsAuthentication.RedirectFromLoginPage(txtUserUsername.Text, chkRemember.Checked);
            break;
        }
        case Helpers.LoginStatus.Blocked:
        {
            Response.Write("<Script> alert('Account Blocked')</Script>");
            break;
        }
        case Helpers.LoginStatus.Invalid:
        {
            Response.Write("<Script> alert('Invalid username or password')</Script>");
            break;
        }
    }
}

In the connection class I am passing the connection string as follows:

public class ConnectionClass
{
    public GameHutDBEntities Entities { get; set; }
    public System.Data.IDbTransaction Transaction { get; set; }

    public ConnectionClass()
    {
        this.Entities = new GameHutDBEntities();
    }

    public ConnectionClass(GameHutDBEntities _Entities)
    {
        this.Entities = _Entities;
    }

    public ConnectionClass(GameHutDBEntities _Entities, string conn)
    {
        this.Entities = _Entities;
        this.Entities.Connection.ConnectionString = conn;
    }
}

After the admin is logged and is redirect into another page the connection string is lost and the default connection string is set? Can someone tell me how to keep the connection string as long as the user logs out.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • using cache or session variables. Is the connectionstring different for each user? Just call your ConnectionClass and cache your values if it's not. – Robert Dec 27 '11 at 15:30
  • Why do you pass it to DAL?! DAL should care about it, not business logic or presentation layer. – abatishchev Dec 27 '11 at 15:35
  • Also I recommend to wrap your UserRepository (BusinessLayer.Users) with MembershipProvider ([Membership.ValidateUser()](http://msdn.microsoft.com/en-us/library/system.web.security.membership.validateuser.aspx)) – abatishchev Dec 27 '11 at 15:37
  • the connection string is different for each user that's the problem – user1114676 Dec 27 '11 at 15:38
  • 1
    Keep all in Web.config under different names. Can't you? – abatishchev Dec 27 '11 at 15:39
  • yes that's what i am doing but i need to store the connection string until the user logs off. – user1114676 Dec 27 '11 at 15:40
  • Off the track. I would suggest using Constructor Chaining in your scenario. The code then becomes more readable and less error-prone. – Manish Basantani Dec 27 '11 at 15:45

2 Answers2

2

That's because you instantiate the class in the web form code behind and the instantiated object is lost when you switch to another page because of ASP.NET statelessness.

If you need to reuse the connection string save it in a session variable:

Session["conn"] = "connection string";

you will be able to reuse it wherever you want.

There are several other options like for example Cache, or a static property, or even read it from Configuration every time you want to use it. It really depends on the context of usage.

EDIT:

To read the connection string from configuration you can use the ConfigurationManager class from System.Configuration:

string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
TheBoyan
  • 6,802
  • 3
  • 45
  • 61
  • the session is not readable in the DataLayer that's the problem – user1114676 Dec 27 '11 at 15:37
  • @user1114676: that's yet another reason why connection string should not cross DAL. – abatishchev Dec 27 '11 at 15:38
  • Then why not reading it from web.config every time you use it? I've updated my answer to reflect on usage. – TheBoyan Dec 27 '11 at 15:41
  • This should still be possible as there is no separate .config file for the class library assemblies. The configuration manager will use the web.config from the application where it is used. I've tried this, it should work. – TheBoyan Dec 27 '11 at 15:45
  • the code that you edited is used in the presentation layer in the login page. I need to save the connectionstring in the data layer in the connection class until the user logs off – user1114676 Dec 27 '11 at 15:53
  • 1
    Why does the presentation layer need to retrieve the connection string? IMO, only the data layer should know about it because only the data layer needs it. And the data layer can use the ConfigurationManager to get it. – Jeff Siver Dec 28 '11 at 07:38
  • @Jeff: +1. DAL can check current user role, and relying on that - choose the proper connection string by name – abatishchev Dec 28 '11 at 07:50
  • @user1114676 If you are using linq I think that this post can help(even if you don't it might give you a hint): http://stackoverflow.com/questions/1188962/linq-to-sql-set-connection-string-dynamically-based-on-environment-variable – TheBoyan Dec 28 '11 at 10:36
0

Keep the connection string name inside data-access layer and read it from Web.config every time you need (or cache it locally).

abatishchev
  • 98,240
  • 88
  • 296
  • 433