|
-
Mar 2nd, 2009, 08:15 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005]Update Connection String
Good Afternoon
My Application is Structured in a Way that before you login you must First Select a Database. Now i have one page that i want to change the Connection String Database name.
I have this function that get called on page load
Code:
/*This Function will change the Connection string in the web.congif file based on the Database that exists on the
* session Variable. If there is nulll then set odirect3 as a current Database name.
*/
public void UpdateConfig()
{
CommonFunctions obj = new CommonFunctions();
String strKey = "NEWDT";
String StrDatabase;
if (Session["ActiveDatabase"] != null)
{
StrDatabase = Convert.ToString(Session["ActiveDatabase"]);
}
else
{
StrDatabase = "oDirectv3";
}
String strValue = @"User id=sa;Password=wow;Server=www; Database="+StrDatabase;
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
if (objAppsettings != null)
{
objAppsettings.Settings[strKey].Value = strValue;
objConfig.Save();
}
}
On the Development site it works well. But while still debugging the code, checking if its doing what i want, There is a point where it has to save the changes
The Part will execute and message box will appear asking me if i really want to save the changes made, i will save yes and everything will be fine. But if i don debug it , it gives an Error that says
Code:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
How can i do this and Suppress this dialogs
Thank you
Last edited by vuyiswamb; Mar 2nd, 2009 at 08:33 AM.
-
Mar 2nd, 2009, 11:00 AM
#2
Re: [2005]Update Connection String
Seeing as it works on your development machine but not when deployed it sounds like it could be a couple of things.
1. It could be permissions based so make sure aspnet_user has write access to the folder with your config file and then try it again.
2. Is the server address/IP address visible from your deployment machine?
-
Mar 4th, 2009, 03:28 AM
#3
Re: [2005]Update Connection String
Your connection string should look like this
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
-
Mar 4th, 2009, 07:29 AM
#4
Thread Starter
Fanatic Member
Re: [2005]Update Connection String
I would like to take a Chance to thank you for your help. The code is working. i created the Following
Code:
/*This Function will change the Connection string in the web.congif file based on the Database that exists on the
* session Variable. If there is nulll then set odirect3 as a current Database name.
*/
private string GetConnectionString(string DB) // where DB is your database name from the session
{
string connString = ConfigurationManager.ConnectionStrings["NEWDT"].ConnectionString;
if (DB != null)
{
return connString.Replace("NEWDT", DB);
}
else
{
return connString.Replace("NEWDT", "oDirectv3");
}
}
and in my Functions i called the above Function like this
Code:
/* This Function will Help you Disable the Grid. What is Passed here is the
*name of the Subject and its going to Return a Pattern of Cycles to disable
*/
public String Disable_Grid_Contents(String Pattern, String DB)
{
strcon = GetConnectionString(DB);
con = new SqlConnection(strcon);
cmdselect = new SqlCommand();
cmdselect.CommandText = "dbo.Get_Pattern";
cmdselect.CommandTimeout = 0;
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.Connection = con;
cmdselect.Parameters.Add("@S", SqlDbType.NVarChar, 63).Value = Pattern;
cmdselect.Parameters.Add("@NS", SqlDbType.NVarChar, 128);
cmdselect.Parameters["@NS"].Direction = ParameterDirection.Output;
String PatternString;
try
{
con.Open();
cmdselect.ExecuteNonQuery();
PatternString = (String)cmdselect.Parameters["@NS"].Value;
}
catch (SqlException)
{
throw;
}
finally
{
if (con != null)
{
con.Close();
}
}
return PatternString;
}
Now i dont need to change the Connection string every time a page run, i will just replace the value of a DB.
Thank you Guys
-
Mar 4th, 2009, 01:04 PM
#5
Re: [RESOLVED] [2005]Update Connection String
Alright, you may want to consider giving your parameters better names though... @S and @NS may confuse whoever looks at this in the future 
Another way to do what you did is to store {0} in the connection string instead of the database and to use String.Format() to replace the {0} with the passed database name.
-
Mar 5th, 2009, 01:11 AM
#6
Thread Starter
Fanatic Member
Re: [RESOLVED] [2005]Update Connection String
Good Point mendhak
I have already changed the named to meaningfull ones and i value you your suggestion.
Thank you Man
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|