EDN Admin
Well-known member
Hi All,<br/>
<br/>
I want to add a text box to my form application to type in the names of the databses in the SQL server and to change the Initial Catalog in the app.config file. My requirement is when I change database name in the text box it should change the app.config I
have according to the database name and do the operations accordingly. My app.config file looks like this;<br/>
<br/>
<?xml version="1.0"?><br/>
<configuration><br/>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><br/>
<connectionStrings><br/>
<br/>
<add name="ConnectionConnector" connectionString="Data Source=MyComputerNameSQLEXPRESS; Initial Catalog=MyFirstDatabase;Persist Security Info = true; Integrated Security = SSPI; " providerName="System.Data.SqlClient" /><br/>
<br/>
</connectionStrings><br/>
<br/>
</configuration><br/>
<br/>
On my form I have a text box(name of which is txtParameters) to take parameters for the program and a button(name of which is doOpertaions). I also have another textbox(name of which is txtdBName) on my form. When I click the button(doOpertaions) it should
first read the database name in the text box(txtdBName) and then change the app.config file and then do the operations accordint to the parameter on the text box(txtParameters). Lets say that the Initial Catalog in my app.config is MyFirstDatabase. When I
change the text of my text box to MySecondDatabase and click the button, the program should do all the operations on the MySecondDatabase. After reading some articles I have included the following code to my doOpertaions_click event on my form. <br/>
<br/>
<pre class="prettyprint private void doOperations_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "MyComputerName\SQLEXPRESS";
builder["Persist Security Info"] = true;
//builder["Integrated Security "] = "SSPI";
builder["Initial Catalog"] = this.txtdBName.Text;
string connectionString = builder.ConnectionString;
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["connectionString"].Value = connectionString;
config.Save();
// do operations on the MySecondDatabase
}[/code]
<br/>
private void doOperations_Click(object sender, EventArgs e)<br/>
{<br/>
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();<br/>
builder["Data Source"] = "MyComputerName\SQLEXPRESS";<br/>
builder["Persist Security Info"] = true;<br/>
//builder["Integrated Security "] = "SSPI";<br/>
builder["Initial Catalog"] = this.txtdBName.Text;<br/>
string connectionString = builder.ConnectionString;<br/>
<br/>
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);<br/>
config.AppSettings.Settings["connectionString"].Value = connectionString;<br/>
config.Save(); <br/>
<br/>
// do operations on the MySecondDatabase<br/>
}<br/>
<br/>
However, the above code does not work. There are two problems.<br/>
<br/>
1. //builder["Integrated Security "] = "SSPI"; gives a Keyword not supported :Integrated Security error.<br/>
<br/>
2. config.AppSettings.Settings["connectionString"].Value = connectionString; give object reference is not set to an instance of an object.<br/>
<br/>
is my approach completely wrong? Is there a different way of doing this? How to eliminate these errors and make it work according to my requirement? <br/>
<br/>
I hope you understand the problem and try to suggest a way to do this. If I can implement this I will be able to use this for all the other databases in my SQL server without having to change the app.config file all the time manually on the Visual Studio. <br/>
<br/>
I hope you can help me out.<br/>
<br/>
Many thanks<br/>
<br/>
Simon<br/>
<br/>
<br/>
View the full article
<br/>
I want to add a text box to my form application to type in the names of the databses in the SQL server and to change the Initial Catalog in the app.config file. My requirement is when I change database name in the text box it should change the app.config I
have according to the database name and do the operations accordingly. My app.config file looks like this;<br/>
<br/>
<?xml version="1.0"?><br/>
<configuration><br/>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><br/>
<connectionStrings><br/>
<br/>
<add name="ConnectionConnector" connectionString="Data Source=MyComputerNameSQLEXPRESS; Initial Catalog=MyFirstDatabase;Persist Security Info = true; Integrated Security = SSPI; " providerName="System.Data.SqlClient" /><br/>
<br/>
</connectionStrings><br/>
<br/>
</configuration><br/>
<br/>
On my form I have a text box(name of which is txtParameters) to take parameters for the program and a button(name of which is doOpertaions). I also have another textbox(name of which is txtdBName) on my form. When I click the button(doOpertaions) it should
first read the database name in the text box(txtdBName) and then change the app.config file and then do the operations accordint to the parameter on the text box(txtParameters). Lets say that the Initial Catalog in my app.config is MyFirstDatabase. When I
change the text of my text box to MySecondDatabase and click the button, the program should do all the operations on the MySecondDatabase. After reading some articles I have included the following code to my doOpertaions_click event on my form. <br/>
<br/>
<pre class="prettyprint private void doOperations_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "MyComputerName\SQLEXPRESS";
builder["Persist Security Info"] = true;
//builder["Integrated Security "] = "SSPI";
builder["Initial Catalog"] = this.txtdBName.Text;
string connectionString = builder.ConnectionString;
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings["connectionString"].Value = connectionString;
config.Save();
// do operations on the MySecondDatabase
}[/code]
<br/>
private void doOperations_Click(object sender, EventArgs e)<br/>
{<br/>
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();<br/>
builder["Data Source"] = "MyComputerName\SQLEXPRESS";<br/>
builder["Persist Security Info"] = true;<br/>
//builder["Integrated Security "] = "SSPI";<br/>
builder["Initial Catalog"] = this.txtdBName.Text;<br/>
string connectionString = builder.ConnectionString;<br/>
<br/>
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);<br/>
config.AppSettings.Settings["connectionString"].Value = connectionString;<br/>
config.Save(); <br/>
<br/>
// do operations on the MySecondDatabase<br/>
}<br/>
<br/>
However, the above code does not work. There are two problems.<br/>
<br/>
1. //builder["Integrated Security "] = "SSPI"; gives a Keyword not supported :Integrated Security error.<br/>
<br/>
2. config.AppSettings.Settings["connectionString"].Value = connectionString; give object reference is not set to an instance of an object.<br/>
<br/>
is my approach completely wrong? Is there a different way of doing this? How to eliminate these errors and make it work according to my requirement? <br/>
<br/>
I hope you understand the problem and try to suggest a way to do this. If I can implement this I will be able to use this for all the other databases in my SQL server without having to change the app.config file all the time manually on the Visual Studio. <br/>
<br/>
I hope you can help me out.<br/>
<br/>
Many thanks<br/>
<br/>
Simon<br/>
<br/>
<br/>
View the full article