Feki Pangestu Wijaya Feki Pangestu Wijaya - 20 days ago 6
MySQL Question

Update variable when data in databases has changed

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)
{

}
}
}

Answer

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

Comments