07-28-2013, 03:01 PM
How hard is it:
private MySqlConnection dbCon = new MySqlConnection();
private void btnLoad_Click(object sender, EventArgs e)
{
try{
//get data from the table
MySqlCommand dbCmd = new MySqlCommand("SELECT * FROM test_table ", dbCon);
MySqlDataReader dbReader = dbCmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dbReader);
//add it to the listbox
listBox1.DataSource = dt;
listBox1.DisplayMember = "test_column2";
//close the reader
dbReader.Close();
dbCmd.Dispose();
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//check if the selected row is less then 0
int row = listBox1.SelectedIndex;
if (row < 0) return;
// get the data from the list
DataTable dt =(DataTable) listBox1.DataSource;
textBox1.Text = dt.Rows[row]["test_column3"].ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
//saving the data
//check if the selected row is less then 0
int row = listBox1.SelectedIndex;
if (row < 0) return;
// get the data from the list
DataTable dt = (DataTable)listBox1.DataSource;
dt.Rows[row]["test_column3"] = textBox1.Text;
MySqlCommand dbCmd = new MySqlCommand("Update test_table SET test_column3=" + textBox1.Text + "WHERE test_column1 =" + dt.Rows[row]["test_column1"] + ";", dbCon);
try{
dbCmd.ExecuteNonQuery();
dbCmd.Dispose();
}catch (Exception ex){
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//make a SQLConnection
string strConnection = @"server=127.0.0.1;database=testdb;uid=root;password=test";
dbCon.ConnectionString = strConnection;
try{
dbCon.Open();
}catch (Exception ex){
MessageBox.Show(ex.Message);
}
}

