Hello C# people,
I have a question; how do i get my values outside of the button?
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQL_verbinding
{
public partial class MainFrame : Form
{
public MainFrame()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=flessensysteem;" +
"UID=root;" +
"PASSWORD=1q2w3e;";
MySqlConnection connection = new MySqlConnection(MyConString);
}
public void get_Click(object sender, EventArgs e)
{
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from flessen";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
string thisrow = "";
for (int i = 0; i < Reader.FieldCount; i++)
thisrow += Reader.GetValue(i).ToString() + ",";
thisrow += ";";
listBox1.Items.Add(thisrow);
}
}
public void close_Click(object sender, EventArgs e)
{
connection.Close();
}
}
}[/code]
error:
[code]Error 1 The name 'connection' does not exist in the current context C:\**\MySQL verbinding\MainFrame.cs 34 36 MySQL verbinding
Error 2 The name 'connection' does not exist in the current context C:\**\MySQL verbinding\MySQL verbinding\MainFrame.cs 37 13 MySQL verbinding
Error 3 The name 'connection' does not exist in the current context C:\**\MySQL verbinding\MySQL verbinding\MainFrame.cs 53 13 MySQL verbinding
[/code]
Loading
MartijnPosted Feb 9, 2009, 4:17 AM
Blocked AccountPosted Feb 9, 2009, 2:13 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace MySQL_verbinding
{
public partial class MainFrame : Form
{
string MyConString = "SERVER=localhost;" +
"DATABASE=flessensysteem;" +
"UID=root;" +
"PASSWORD=1q2w3e;";
MySqlConnection connection = new MySqlConnection(MyConString);
public MainFrame()
{
InitializeComponent();
}
}
}
MartijnPosted Feb 6, 2009, 7:26 AM
Can you give me an example? sinds im just starting with this, i understand code words a bit better..
please =)
Blocked AccountPosted Feb 6, 2009, 6:42 AM
Hi,
You have define the connection variable which is out of scope means that u can't use that in other place instead of Button1_Click.
If u want to use that every where then define it in the class level.