Results 1 to 8 of 8

Thread: What's wrong with this code???

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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:
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. using System.Windows.Forms;
    5.  
    6. class Database
    7. {
    8.     public static void db_open(string ConnectionStringPayload)
    9.     {
    10.         if(ConnectionStringPayload == "")
    11.             MessageBox.Show("Connection Error.", "Invalid Connection String", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    12.         else
    13.             try
    14.             {
    15.                 SqlConnection myConnection = new SqlConnection(ConnectionStringPayload);
    16.                 myConnection.Open();
    17.             }
    18.             catch
    19.             {
    20.                 MessageBox.Show("Connection Error", "Cannot connect to database, please check the connection string.");
    21.             }
    22.     }
    23.    
    24.     public static string db_state()
    25.     {
    26.         string state = "";
    27.         [color=red]if(myConnection.State.ToString() == "Open") ' errors here[/color]
    28.             state = "Open";
    29.         else
    30.             state = "Closed";
    31.            
    32.         return state;  
    33.            
    34.     }
    35.    
    36.     public static void Main()
    37.     {
    38.         string myConnectionString = ("server=localhost;database=pubs;user id=xxx;password=xxx;");
    39.         db_open(myConnectionString);
    40.         Console.WriteLine("Connection State: " + db_state());
    41.     }
    42. }
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  2. #2
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    You did not declare and initialize myConnection as a variable.

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

  3. #3

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  4. #4
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    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.

  5. #5

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  7. #7

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Schweet!...Thanks guys.
    Last edited by Memnoch1207; Sep 30th, 2003 at 08:22 AM.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
  •  



Click Here to Expand Forum to Full Width