Monday, March 26, 2012

One Application With 2 DataBase

Hai All ,

Iam having the one Application in DotNet2005.But iam using the two database(SqlServer2005).If user's login the application it will check credentials in first database, if he is not having the login credentials it will cheek the second Database. How can I handle in the configuration file?. How can I change the connection string depending upon the Database in my application for database operation (like insert and update).Any one kindly provide the solution.

Thanks and regards,sureshK

you can have 2 separate connection string in web.config file, appsetting section like

<add key="loginConnectionString" value = "" />

<add key="ConnectionString" value="" />

you can check have a Session Variable named as "CurrentLogin" where you can store the ID or Name or anything user related information after successful login.. if user is logged in.. then that Session Variable must having Some Value.. you can check that value and;... set and use required Connection String by you... hope i got your situation...

|||

Hai Thank You. Your answer is exactly Correct. If their is any other way to achieve this?.

With Regards,

SureshK.|||

Hi mskumarmca,

Just askaushalparik27 has suggested above, you can use two different connection string to connect to 2 databases, and in your code behind, first build one connecton to the first database and execute your query. If the specific username/password has been found, it indicates that the current user is registered in the first dabase. And if not, you can build another connection to the second database and execute the same query likewise.

A small tip here: you can provide 2 check boxes in the page UI, giving users a change to select on which database they would like to verify their credential. This should be much helpful to enhance your site performance.

Hope my suggestion helps

|||

Hi thank for your tips.

Yes I get database name and passing the database name as parameter and finished.

But I am trying to keep the Database name in session. But I am not able to get that session value in my BL layer.

For change the connection string for further database operations. Below code was I am trying to get database value in BL Layer class file. But the session value is set as Null. What is the problem?

String DBName=(String)System.Web.HttpContext.Current.Session["DBNAME"];

Kindly give me the idea. Regards,SureshK|||

where and how did you store that DBNAME into your session variables?

|||

Hi thanks for your reply,

When the user log in the application. I will connect through default connection string with DB1.

I have one SP to check login cardinals in DB1 and DB2 and it will return the login cardinals and connection string name.

if user name is DB1 I am going to use ConsStr1 else Constr2. That Connection string value in login page.

Session["DBNAME"] = dsLogin.Tables[0].Rows[0][DBNAME].ToString();

Above session value only i need to access in my business logic layer.

Regards,

SureshK

|||

Hai all I Got the answer.

In BL Class file .

Import the namesapce.

using System.Web.SessionState;

And Following is to access the session Variable.

string SapId = (String)System.Web.HttpContext.Current.Session["session variale name in asp.cs"];

No comments:

Post a Comment