Tuesday 6 August 2013

StreamReader null in class

StreamReader null in class

I have got this method which creates SqlConnection string in class. I read
the data from .txt file but the problém is that if there is no data
inserted the app doesn't even turn on. I was wondering how can I thrown an
exception in messagebox and insert that command right into this class
method. I'm not sure if it is possible. Sorry if my answer is too trivial
or it's non-sense. I'm not programming for long time.
internal static class DataSource
{
private static string _ConnectionString;
public static string ConnectionString
{
get
{
if (_ConnectionString == null)
_ConnectionString =
FunctionToDynamicallyCreateConnectionstring();
return _ConnectionString;
}
}
private static string FunctionToDynamicallyCreateConnectionstring()
{
string path = "C:\\Users\\marek\\Documents\\Visual Studio
2012\\Projects\\tours\\tours\\sql_string.txt";
StreamReader sr = new StreamReader(File.Open(path,
FileMode.Open));
SqlConnectionStringBuilder cb = new
SqlConnectionStringBuilder();
cb.DataSource = sr.ReadLine();
cb.InitialCatalog = sr.ReadLine();
cb.UserID = sr.ReadLine();
cb.Password = sr.ReadLine();
return cb.ToString();
}

No comments:

Post a Comment