Question

This is my simple code just to read something from MySQL. But want I want is to create connection and command when Form is opened and just to open connection when button is clicked and do the rest. But It says:

"The name 'konekcija' does not exist in the current context"

Could someone explain me please.

namespace mysql_windows_console
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            /*========MYSQL KONEKCIJA===========*/
            string baza               = "server=localhost;database=test;user=root;password=;";
            MySqlConnection konekcija = new MySqlConnection(baza);
            MySqlCommand comm         = konekcija.CreateCommand();
            /*========MYSQL KONEKCIJA===========*/
        }

        private void button1_Click(object sender, EventArgs e)
        {
            konekcija.Open();
            string sql               = "SELECT IME,PREZIME FROM tabela";
            MySqlDataAdapter adapter = new MySqlDataAdapter(sql,konekcija);
            DataTable tab            = new DataTable();
            adapter.Fill(tab);
            dataGridView1.DataSource = tab;
            konekcija.Close();
        }
    }
}
Was it helpful?

Solution

You should declare MySqlConnection outside of the Form_Load EventHandler so you can access it from other methods. Also I would suggest to initialize it in a Form constructor. Since you are using DataAdapter you don't need to use MySqlCommand. Here is the revised code;

namespace mysql_windows_console
{
public partial class Form1 : Form
{
    MySqlConnection konekcija;
    string baza = "server=localhost;database=test;user=root;password=;"; //so you can access it again if you need it b any chance
    public Form1()
    {
        InitializeComponent();
        konekcija = new MySqlConnection(baza);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        konekcija.Open();
        string sql = "SELECT IME,PREZIME FROM tabela";
        MySqlDataAdapter adapter = new MySqlDataAdapter(sql,konekcija);
        DataTable tab = new DataTable();
        adapter.Fill(tab);
        dataGridView1.DataSource = tab;
        konekcija.Close();
    }
}

}

OTHER TIPS

You're storing konekcija as a local variable. Make it a property like:

private MySqlConnection konekcija { get; set; }

public void Form1_Load(object sender, EventArgs e)
{
    //...
    this.konekcija = new MySqlConnection(baza);
}

private void button1_click(object sender, EventArgs e)
{
    this.konekcija.Open();
    //...
}

it simply means that konekcija is not found on the scope of button1_Click.

Minimize the scope of the variable as much as possible. Why not connect only when needed? Example,

const string baza = "server=localhost;database=test;user=root;password=;";  
private void button1_Click(object sender, EventArgs e)
{

    using (MySqlConnection _conn = new MySqlConnection(baza))
    {
        using (MySqlCommand _comm = new  MySqlCommand())
        {
            _comm.Connection = _conn;
            _comm.CommandText = "SELECT IME,PREZIME FROM tabela";
            _comm.CommandType = CommandType.Text;

            using (MySqlDataAdapter _adapter = new MySqlDataAdapter(_comm))
            {
                DataTable _table = new DataTable;
                try
                {
                    _adapter.Fill(_table);
                    dataGridView1.DataSource = _table;
                }
                catch (MySqlException e)
                {
                    MessageBox.Show(e.Message.ToString());
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top