private void loadTable() //load values to table { //clear data grid view dgvItems.Rows.Clear(); DataSet ds = new DataSet(); MySQLconnection con = new MySQLconnection(); //create connection //create sql query for get data string sqlQuery = "SELECT * FROM items"; //Executing ds = con.LoadData(sqlQuery); //check if data data is available if (ds.Tables[0].Rows.Count == 0) { return; } for (int n = 0; n < ds.Tables[0].Rows.Count; n++) //loopping through add data to dgv { itemBindingSource.Add(new item { ID = ds.Tables[0].Rows[n]["ID"].ToString(), ProductName = ds.Tables[0].Rows[n]["productName"].ToString(), Price = Convert.ToDouble(ds.Tables[0].Rows[n]["productPrice"].ToString()), isActive = Convert.ToBoolean(Convert.ToInt32(ds.Tables[0].Rows[n]["isActive"].ToString())) }); } }
private void btnAdd_Click(object sender, EventArgs e) { try { if (txtbPrice.Text != string.Empty && txtProductName.Text != string.Empty) //check if all fields are filled { string isActive = chkActive.Checked ? "1" : "0"; string sqlQuery = "INSERT INTO items (productName, productPrice, isActive ) VALUES ('" + txtProductName.Text + "','" + txtbPrice.Text + "', '" + isActive + "'); "; //insert data MySQLconnection con = new MySQLconnection(); con.Executing(sqlQuery); loadTable(); } else { MessageBox.Show("Please fill all the fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
private void deleteData(int row) { string ID = dgvItems.Rows[row].Cells[0].Value.ToString(); string sqlQuery = "DELETE FROM items WHERE ID = '" + ID + "';"; MySQLconnection con = new MySQLconnection(); con.Executing(sqlQuery); //Load Table loadTable(); }
private void btnSave_Click(object sender, EventArgs e) { try { if (txtbPrice.Text != string.Empty && txtProductName.Text != string.Empty) //check if all fields are filled { string isActive = chkActive.Checked ? "1" : "0"; string sqlQuery = "UPDATE items SET productName = '" + txtProductName.Text + "', productPrice = '" + txtbPrice.Text + "', isActive = '" + isActive + "' WHERE ID = '" + Item.ID + "';"; //update data MySQLconnection con = new MySQLconnection(); con.Executing(sqlQuery); this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }