tags:

views:

752

answers:

5

Okay this question is so basic it is probably in my book head first c#, but alas I am lazy :) Actually I read it and sort of understand and sort of don't. Here is what I have.

I am trying to save data to a database on a button push but the variables seem to be private by the nature of where they are defined. I have tried to move where they are defined, but this seems to produce other errors. I am sure someone can answer this in under 5 seconds and I do appreciate the help. Plus I know stackoverflow won't turn into a flame war becasue the question is so stupid.

Also if you are feeling genourous if you could explain to me (and others who see this behind me) not just how to fix it but why it was fixed that way. The code follows.

namespace enable
{    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, favouriteConnection);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(adapter);
            DataTable dTable = new DataTable();
            adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            adapter.Update(dTable);            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }
    }
}
A: 

Update: [sigh] I forgot to move dTable to the class cope as well...

namespace enable
{    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            m_Adapter = new OleDbDataAdapter(strSQL, favouriteConnection)l
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(m_Adapter);
            dTable = new DataTable();
            m_Adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            m_Adapter.Update(dTable);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_Adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }

        OleDbDataAdapter m_Adapter;
        DataTable dTable;
    }
}
Franci Penov
You missed the data table
AlbertEin
You have OleDbDataAdapter in the namespace block.
FlySwat
There you fixed it :)
FlySwat
yeah, that's what happens when I use Notepad for code editing... :-)
Franci Penov
+8  A: 

You're declaring dTable and adapter in the constuctor, so it goes out of scope as soon as the constructor is completed.

You want to move the variable declarations out into the main class, like:

public partial class Form1 : Form
{
    private DataTable dTable;
    private OleDbDataAdapter adapter;

    Public Form1()
    {
         ... your setup here ...
         dTable = new DataTable();
         ... etc ...
    }

}
C. Lawrence Wenham
Explanation: basically you're saying "Give me an adapter object that I will use in this method (the constructor)" Implicit to that is that when the method ends you don't need it anymore. If you do as recommended you say "I will use an adapter object in this class". The keyword to look up is scope
George Mauer
+1  A: 

adapter is scoped to the constructor of Form1, not to the class itself.

Move adapter and dtable to be private members of the class.

FlySwat
+2  A: 
namespace enable
{    
    public partial class Form1 : Form
    {

    OleDbDataAdapter adapter;
    DataTable dTable = new DataTable();

        public Form1()
        {
            InitializeComponent();
            OleDbConnection favouriteConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\192.168.123.5\\Share\\Matt\\BugTypes.mdb");
            string strSQL = "SELECT CategoryName, Show " + "FROM [Categories] WHERE Show = 'Yes' " + "ORDER BY CategoryName";
            adapter = new OleDbDataAdapter(strSQL, favouriteConnection);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(adapter);
            adapter.Fill(dTable);
            BindingSource bSource = new BindingSource();
            bSource.DataSource = dTable;
            dataGridView1.DataSource = bSource;
            adapter.Update(dTable);            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            adapter.Update(dTable);//this is the button that needs to do the save, but can't see the variables.
        }
    }
}

You need to change DataAdapter and the dataTable scope to be accesible to the button click method event. If you declare them on the constructor they cannot be acceced on other methods, you need to declare them as object fields to be "global" to your object instance.

You need to find out what scope need each variable, you can have a local scope, that is, declared inside a method or a class scope, declared outside a method.

AlbertEin
A: 

adapter and dTable is declared within your constructor. They should both be 'moved out' of the constructor to get class wide scoop. Just as Franci did with the adapter.

There might be other errors but it is hard to guess when you haven't posted your compiler error.

/johan/

idstam