2

Here I have three variables, and the variables are filled by the data from the database and displays it to the screen using the label. But when I change the data in the database, the variables are not automatically changed. What would I try to do is create a variable to change automatically when the data in the database changes. But I do not know how to do because I'm new at this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace vendingInterface
{
    public partial class Form1 : Form
    {

        public MySqlConnection conn;
        private string server;
        private string database;
        private string uid;
        private string password;
        private string produk11;
        private string produk22;
        private string produk33;


        public Form1()
        {
            bacadatabase();
            InitializeComponent();
            info();

        }

I display it using the label

        private void info()
        {
            lbl_produk1.Text = "Jumlah Produk 1 = " + produk11 + " Buah";
            lbl_produk2.Text = "Jumlah Produk 2 = " + produk22 + " Buah";
            lbl_produk3.Text = "Jumlah Produk 3 = " + produk33 + " Buah";
        }

I took the data in the database

        public void bacadatabase()
        {
            string sql = "select *from vanding_machine";
            server = "localhost";
            database = "db_s2u";
            uid = "root";
            password = "";

            string connString = "SERVER=" + server + ";" + " DATABASE=" + database + ";" + " UID=" + uid + ";" + " PASSWORD=" + password + ";";  //conek SQL
            MySqlConnection conn = new MySqlConnection(connString);


            conn.Open();
            //MessageBox.Show("Koneksi Berhasil");

            //command
            MySqlCommand cmd = new MySqlCommand(sql, conn);

            //reader
            MySqlDataReader dataReader = cmd.ExecuteReader();

            while (dataReader.Read())
            {
                produk11 = dataReader["produk1"].ToString();
                produk22 = dataReader["produk2"].ToString();
                produk33 = dataReader["produk3"].ToString();
            }


            dataReader.Close();
            conn.Close();

        }

When I click the button the data will be entered into the database and variables in the show will be updated automatically

        private void button9_Click(object sender, EventArgs e)
        {

            server = "localhost";
            database = "db_s2u";
            uid = "root";
            password = "";

            string connString = "SERVER=" + server + ";" + " DATABASE=" + database + ";" + " UID=" + uid + ";" + " PASSWORD=" + password + ";";  //conek SQL
            MySqlConnection conn = new MySqlConnection(connString);


            conn.Open();
            //MessageBox.Show("Koneksi Berhasil");


            string sql = "update vanding_machine set produk1 = @produk111;";

            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add(new MySqlParameter("@produk111", tb_produk1.Text));

            //close data reader
            cmd.ExecuteNonQuery();

            //close connection
            conn.Close();

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }
    }
}
9
  • OK, so you read some values at some point, but you have to go check if those values change, SQL does not notify on change Commented Nov 15, 2016 at 8:16
  • @BugFinder then how can I know if the data in the database change if sql not give notice. Do you have a solution? because I do not have a solution Commented Nov 15, 2016 at 8:29
  • are you inserting from your form to the database, do you check your database to ensure what you have inserted is there Commented Nov 15, 2016 at 9:32
  • yes I inserting from my form. Yes I also check it @sea50 Commented Nov 15, 2016 at 9:55
  • load your database data to a dataGridView then use the cellClick event to display your info to a form.....long short but will work :) Commented Nov 15, 2016 at 10:22

1 Answer 1

1

You will have to create timer to check whether was data changed or not in application. The database will not point up the application about any change

Sign up to request clarification or add additional context in comments.

2 Comments

I do not know how to create it. Can you explain it clearly to me @Erik Šťastný
I have tried it and it work properly. thank you @Erik Šťastný

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.