Click here to Skip to main content
15,900,439 members
Home / Discussions / C#
   

C#

 
Generalarrange dataGrid Columns Pin
Adnan Siddiqi26-Feb-05 10:53
Adnan Siddiqi26-Feb-05 10:53 
GeneralRe: arrange dataGrid Columns Pin
Heath Stewart27-Feb-05 4:30
protectorHeath Stewart27-Feb-05 4:30 
GeneralSystem.Xml.XmlException: The root element is missing. Pin
mantissaPuc26-Feb-05 9:53
mantissaPuc26-Feb-05 9:53 
GeneralRe: System.Xml.XmlException: The root element is missing. Pin
Radgar26-Feb-05 16:59
Radgar26-Feb-05 16:59 
GeneralProblem with my DB.. Pin
Tugbay Sahin26-Feb-05 9:14
Tugbay Sahin26-Feb-05 9:14 
GeneralRe: Problem with my DB.. Pin
Colin Angus Mackay26-Feb-05 9:20
Colin Angus Mackay26-Feb-05 9:20 
GeneralRe: Problem with my DB.. Pin
Tugbay Sahin26-Feb-05 9:27
Tugbay Sahin26-Feb-05 9:27 
GeneralRe: Problem with my DB.. Pin
Colin Angus Mackay26-Feb-05 9:41
Colin Angus Mackay26-Feb-05 9:41 
The reson for the error message is that myConnection is not defined within the current scope. Scope can be simply thought of as the code between the braces { }. As you will have noticed a C# program has lots of opening and closing braces like this...
namespace MyNamespace
{
    class MyClass
    {
        public void MyMethod()
        {
            for(int i=0;i<100;i++)
            {
                Console.WriteLine(i.ToString());
            }
        }
    }
}
Before each opening brace is some code that explains what the scope is for. e.g. class MyClass or public void MyMethod() to indicate the start of a level of scope.

In your original post you declared myConnection in the constructor, so it was local to the scope of the constructor. What you need to do is declare myConnection in the scope of the class (because you can only see what is in your scope + the parent(s) scope) then you can set value in the constructor.

For example:
namespace MyNamespace
{
    class MyClass
    {
        // Declare myConnection in the class (a class member variable, AKA a field)
        private SqlConnection myConnection;

        public void MyClass()
        {
            // Create a connection object and assign it to the member variable
            myConnection = new SqlConnection(....);
        }
        private void button2_Click(object sender, System.EventArgs e)
        {
            SqlCommand myCommand = new SqlCommand("Command String", myConnection);
        }
    }
}


Does this help?


Cada uno es artifice de su ventura
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums


GeneralRe: Problem with my DB.. Pin
Tugbay Sahin26-Feb-05 9:43
Tugbay Sahin26-Feb-05 9:43 
GeneralRe: Problem with my DB.. Pin
Rob Graham26-Feb-05 9:41
Rob Graham26-Feb-05 9:41 
Generalescaping special characters... Pin
theJazzyBrain26-Feb-05 8:51
theJazzyBrain26-Feb-05 8:51 
GeneralRe: escaping special characters... Pin
leppie26-Feb-05 9:09
leppie26-Feb-05 9:09 
GeneralRe: escaping special characters... Pin
theJazzyBrain27-Feb-05 11:26
theJazzyBrain27-Feb-05 11:26 
GeneralRe: escaping special characters... Pin
Dave Kreskowiak26-Feb-05 9:37
mveDave Kreskowiak26-Feb-05 9:37 
GeneralRe: escaping special characters... Pin
theJazzyBrain27-Feb-05 1:37
theJazzyBrain27-Feb-05 1:37 
GeneralRe: escaping special characters... Pin
Dave Kreskowiak27-Feb-05 3:12
mveDave Kreskowiak27-Feb-05 3:12 
GeneralRe: escaping special characters... Pin
theJazzyBrain27-Feb-05 11:26
theJazzyBrain27-Feb-05 11:26 
GeneralLan Messenger in C# Pin
naq26-Feb-05 8:29
naq26-Feb-05 8:29 
GeneralRe: Lan Messenger in C# Pin
Dave Kreskowiak26-Feb-05 9:35
mveDave Kreskowiak26-Feb-05 9:35 
GeneralRe: Lan Messenger in C# Pin
qnzone27-Feb-05 8:20
qnzone27-Feb-05 8:20 
GeneralShared Ressource? between main and thread for keeping track of status Pin
Shaitan0026-Feb-05 8:20
Shaitan0026-Feb-05 8:20 
GeneralHTTP protocol violation problem Pin
Small Rat26-Feb-05 6:59
Small Rat26-Feb-05 6:59 
GeneralRe: HTTP protocol violation problem Pin
Radgar26-Feb-05 9:34
Radgar26-Feb-05 9:34 
QuestionHow to bypass proxy/firewall in .Net? Pin
Indian Ocean26-Feb-05 4:41
Indian Ocean26-Feb-05 4:41 
AnswerRe: How to bypass proxy/firewall in .Net? Pin
Colin Angus Mackay26-Feb-05 9:23
Colin Angus Mackay26-Feb-05 9:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.