I have a Web setup project. In the setup I have an input field where the user can insert a connectionstring. When I run the setup I get this error:
Error 1001. Unknown error (0x8000x5000)
To track where the error exists I create a file and in every method I write something to this file. Now I think the error is raised by this line:
string friendlySiteName = entry.Properties["ServerComment"].Value.ToString();
But I don't know how to correct this problem. I hope you can help me out!
Code:
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
// Retrieve configuration settings
string targetSite = Context.Parameters["targetsite"];
string targetVDir = Context.Parameters["targetvdir"];
string targetDirectory = Context.Parameters["targetdir"];
string targetConnectionString = Context.Parameters["targetconn"];
FileStream f = new FileStream("c:\\myfile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter w = new StreamWriter(f);
w.WriteLine("targetSite: " + targetSite);
w.WriteLine("targetVDir: " + targetVDir);
w.WriteLine("targetDirectory: " + targetDirectory);
w.WriteLine("targetConnectionString: " + targetConnectionString);
w.Close();
w.Dispose();
f.Close();
f.Dispose();
ConfigureWebConfig(targetSite, targetVDir, targetConnectionString);
}
void ConfigureWebConfig(string targetSite, string targetVDir, string targetConn)
{
try
{
// Retrieve "Friendly Site Name" from IIS for TargetSite
DirectoryEntry entry = new DirectoryEntry("IIS://LocalHost/" + targetSite);
FileStream f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
StreamWriter w = new StreamWriter(f);
w.WriteLine("In ConfigureWebConfig stap 1 ");
w.Close();
w.Dispose();
f.Close();
f.Dispose();
string friendlySiteName = entry.Properties["ServerComment"].Value.ToString();
f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
w = new StreamWriter(f);
w.WriteLine("In ConfigureWebConfig friendlySiteName: " + friendlySiteName);
w.Close();
w.Dispose();
f.Close();
f.Dispose();
// Open Application's Web.Config
Configuration config = WebConfigurationManager.OpenWebConfiguration("/" + targetVDir, friendlySiteName);
addConnectionStringAttribute(targetConn, config);
f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
w = new StreamWriter(f);
w.WriteLine("In ConfigureWebConfig stap 2 ");
w.Close();
w.Dispose();
f.Close();
f.Dispose();
// togleCompilationAttribute(config);
// Persist web.config settings
config.Save();
f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
w = new StreamWriter(f);
w.WriteLine("In ConfigureWebConfig stap 3 ");
w.Close();
w.Dispose();
f.Close();
f.Dispose();
}
catch (Exception)
{
throw;
}
}
private static void addConnectionStringAttribute(string connectionStringValue, Configuration config)
{
ConnectionStringSettings appDatabase = new ConnectionStringSettings();
appDatabase.Name = "dataConnectionString";
appDatabase.ConnectionString = connectionStringValue;
appDatabase.ProviderName = "System.Data.SqlClient";
FileStream f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
StreamWriter w = new StreamWriter(f);
w.WriteLine("In addConnectionStringAttribute stap 1 ");
w.Close();
w.Dispose();
f.Close();
f.Dispose();
config.ConnectionStrings.ConnectionStrings.Clear();
config.ConnectionStrings.ConnectionStrings.Add(appDatabase);
f = new FileStream("c:\\myfile.txt", FileMode.Append, FileAccess.Write);
w = new StreamWriter(f);
w.WriteLine("In addConnectionStringAttribute stap 2 ");
w.Close();
w.Dispose();
f.Close();
f.Dispose();
}