Wednesday, 14 August 2013

IO.directorynotfound - Dynamical SqlConnection string

IO.directorynotfound - Dynamical SqlConnection string

I have got this code which should read path from app.Settings but now I
have value there and when I release this project as .exe file and I try to
install it on another computer after successful instalation I run it and
it throws an error with system.IO.directorynotfound .
I thought that it can look something like this:
private static string _ConnectionString;
public static string ConnectionString
{
get
{
if (_ConnectionString == null)
_ConnectionString =
FunctionToDynamicallyCreateConnectionstring();
return _ConnectionString;
}
}
private static string FunctionToDynamicallyCreateConnectionstring()
{
string path = Properties.Settings.Default.swPath;
if (path != null)
{
StreamReader sr = new StreamReader(File.Open(path,
FileMode.Open));
SqlConnectionStringBuilder cb = new
SqlConnectionStringBuilder();
cb.DataSource = DecodeFrom64(sr.ReadLine());
cb.InitialCatalog = DecodeFrom64(sr.ReadLine());
cb.UserID = DecodeFrom64(sr.ReadLine());
cb.Password = DecodeFrom64(sr.ReadLine());
}
return cb.ToString();
}
But it gives following error: The name 'cb' does not exist in the current
context - yes I know why because it is not inside the arrange of If. But
Im not sure how to improve it so if the path wasn't found on particular
computer the program will normally continue until I set the right path.
Thank you everyone for your time and answers.

No comments:

Post a Comment