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()); } }




Reply With Quote