PDA

Click to See Complete Forum and Search --> : What's wrong with this code???


Memnoch1207
Sep 29th, 2003, 03:23 PM
The code below keeps generating the following error and I don't know why???

testDatabaseModules.cs(28,6): error CS0246: The type or namespace name 'myConnection' could not be found (are you missing a using directive or an assembly reference?)


using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

class Database
{
public static void db_open(string ConnectionStringPayload)
{
if(ConnectionStringPayload == "")
MessageBox.Show("Connection Error.", "Invalid Connection String", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
try
{
SqlConnection myConnection = new SqlConnection(ConnectionStringPayload);
myConnection.Open();
}
catch
{
MessageBox.Show("Connection Error", "Cannot connect to database, please check the connection string.");
}
}

public static string db_state()
{
string state = "";
if(myConnection.State.ToString() == "Open") ' errors here
state = "Open";
else
state = "Closed";

return state;

}

public static void Main()
{
string myConnectionString = ("server=localhost;database=pubs;user id=xxx;password=xxx;");
db_open(myConnectionString);
Console.WriteLine("Connection State: " + db_state());
}
}

aewarnick
Sep 29th, 2003, 04:09 PM
You did not declare and initialize myConnection as a variable.

Bitmap b=new Bitmap(10, 10);
b.YouCanUseMeNow!();

Memnoch1207
Sep 29th, 2003, 04:33 PM
I tried

public static string db_state()
{
string state = "";
myConnection conn = new myConnection();

if(conn.State.ToString() == "Open")
state = "Open";
else
state = "Closed";

return state;
}


but am still getting the following errors

testDatabaseModules.cs(28,3): error CS0246: The type or namespace name 'myConnection' could not be found (are you missing a using directive or an assembly reference?)

testDatabaseModules.cs(30,6): error CS0246: The type or namespace name 'conn' could not be found (are you missing a using directive or an assembly reference?)

aewarnick
Sep 29th, 2003, 04:41 PM
It does not know myConnection. It must be in some other class or namespace.

Where is your code there located and where is the class myConnection located?

The Namespace and Class of each.

Memnoch1207
Sep 29th, 2003, 07:32 PM
everything is in the first post...
the myConnection is created in the public static void db_open sub...SqlConnection myConnection = new SqlConnection.

sunburnt
Sep 29th, 2003, 07:54 PM
myConnection is created inside the try{} block. That is its scope. Once the try block ends, myConnection is destroyed. You need to declare it as a static member of the class like so:


using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

class Database
{
private static SqlConnection myConnection;

public static void db_open(string ConnectionStringPayload)
{
if(ConnectionStringPayload == "")
MessageBox.Show("Connection Error.", "Invalid Connection String", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
try
{
myConnection = new SqlConnection(ConnectionStringPayload);
myConnection.Open();
}
catch
{
MessageBox.Show("Connection Error", "Cannot connect to database, please check the connection string.");
}
}

public static string db_state()
{
string state = "";
if(myConnection.State.ToString() == "Open") ' errors here
state = "Open";
else
state = "Closed";

return state;

}

public static void Main()
{
string myConnectionString = ("server=localhost;database=pubs;user id=xxx;password=xxx;");
db_open(myConnectionString);
Console.WriteLine("Connection State: " + db_state());
}
}

Memnoch1207
Sep 30th, 2003, 08:14 AM
Schweet!...Thanks guys.

DevGrp
Sep 30th, 2003, 05:03 PM
For the Connection State you dont have to call the ToString method. You can do this

if(myConnection.State == ConnectionState.Open)
{
...
}