Very simple problem (does not exist in the class or namespace)

M

Mark Allison

Hi,

VERY simple problem!

if (regServer.UseTrustedConnection == 1)
{
string connectionString = "server=" + regServer.Name + ";
Trusted_Connection=yes; database=master";
}
else
{
string connectionString = "server= " + regServer.Name +
";uid="+regServer.Login+";password="+regServer.Password+";database=master";
}
Console.WriteLine (connectionString);

I get an error on the last line saying:

The name 'connectionString' does not exist in the class or namespace.
Why is that? I just declared it in my if statement.

Thanks.

--

Mark Allison, SQL Server MVP
http://www.markallison.co.uk

Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
 
M

Morten Wennevik

Hi Mark,

This is because you define connectionString inside an if block and C# does not allow it to fall through the rest of the code.

Change your code to

string connectionString = "";

if (regServer.UseTrustedConnection == 1)
{
connectionString = "server=" + regServer.Name + "; Trusted_Connection=yes; database=master";
}
else
{
connectionString = "server= " + regServer.Name +
";uid="+regServer.Login+";password="+regServer.Password+";database=master";
}

Console.WriteLine (connectionString);
 
G

Glenn

Mark

Put 'string connectionString = string.Empty;' prior to the if statement.

Glenn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top