Shops loading error
#8

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


Messages In This Thread
[No subject] - by Jaiz - 07-28-2013, 10:48 AM
[No subject] - by someone - 07-28-2013, 11:38 AM
[No subject] - by Jaiz - 07-28-2013, 12:06 PM
[No subject] - by soryjero - 07-28-2013, 12:13 PM
[No subject] - by someone - 07-28-2013, 12:21 PM
[No subject] - by Jaiz - 07-28-2013, 12:25 PM
[No subject] - by Jaiz - 07-28-2013, 12:58 PM
[No subject] - by someone - 07-28-2013, 03:01 PM
[No subject] - by Jaiz - 07-28-2013, 04:41 PM
[No subject] - by someone - 07-28-2013, 05:07 PM
[No subject] - by Jaiz - 07-28-2013, 05:35 PM
[No subject] - by someone - 07-29-2013, 04:40 PM
[No subject] - by Jaiz - 07-29-2013, 04:52 PM
[No subject] - by soryjero - 07-29-2013, 10:26 PM
[No subject] - by Jaiz - 07-30-2013, 06:43 PM
[No subject] - by soryjero - 07-30-2013, 10:34 PM
[No subject] - by Jaiz - 07-31-2013, 01:01 PM
[No subject] - by soryjero - 07-31-2013, 09:59 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)