|
-
Sep 29th, 2003, 03:23 PM
#1
Thread Starter
Frenzied Member
What's wrong with this code???
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?)
VB Code:
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 = "";
[color=red]if(myConnection.State.ToString() == "Open") ' errors here[/color]
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());
}
}
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 29th, 2003, 04:09 PM
#2
Frenzied Member
You did not declare and initialize myConnection as a variable.
Bitmap b=new Bitmap(10, 10);
b.YouCanUseMeNow!();
-
Sep 29th, 2003, 04:33 PM
#3
Thread Starter
Frenzied Member
I tried
Code:
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?)
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 29th, 2003, 04:41 PM
#4
Frenzied Member
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.
-
Sep 29th, 2003, 07:32 PM
#5
Thread Starter
Frenzied Member
everything is in the first post...
the myConnection is created in the public static void db_open sub...SqlConnection myConnection = new SqlConnection.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 29th, 2003, 07:54 PM
#6
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:
Code:
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());
}
}
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Sep 30th, 2003, 08:14 AM
#7
Thread Starter
Frenzied Member
Last edited by Memnoch1207; Sep 30th, 2003 at 08:22 AM.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 30th, 2003, 05:03 PM
#8
Frenzied Member
For the Connection State you dont have to call the ToString method. You can do this
Code:
if(myConnection.State == ConnectionState.Open)
{
...
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|