Domanda

Sto cercando di salvare i dati in un database con la semplice pressione di un pulsante, ma le variabili sembrano private per la natura di dove sono definite. Ho provato a spostarmi dove sono definiti, ma questo sembra produrre altri errori.

Data una soluzione, perché è stato risolto in questo modo?

Il codice segue.

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.
        }
    }
}
È stato utile?

Soluzione

Stai dichiarando dTable e adapter nel costruttore, quindi esce dal campo di applicazione non appena il costruttore è completato.

Desideri spostare le dichiarazioni delle variabili nella classe principale, come:

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

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

Altri suggerimenti

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.
        }
    }
}

È necessario modificare DataAdapter e l'ambito dataTable in modo che siano accessibili all'evento del metodo clic pulsante. Se li dichiari sul costruttore non possono essere accecati con altri metodi, devi dichiararli come campi oggetto per essere "globali". all'istanza dell'oggetto.

Devi scoprire quale ambito necessita di ogni variabile, puoi avere un ambito locale, cioè dichiarato all'interno di un metodo o un ambito di classe, dichiarato al di fuori di un metodo.

L'adattatore

è mirato al costruttore di Form1, non alla classe stessa.

Sposta l'adattatore e la dtable in modo che siano membri privati ??della classe.

Aggiornamento: [sospiro] Ho dimenticato di spostare dTable anche sul fronte di classe ...

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

adapter e dTable sono dichiarati nel costruttore. Dovrebbero essere entrambi 'spostati' fuori dal costruttore per ottenere uno scoop di classe. Proprio come ha fatto Franci con l'adattatore.

Potrebbero esserci altri errori ma è difficile indovinare quando non si è registrato l'errore del compilatore.

/ Johan /

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top