user3536490
user3536490

Reputation: 7

How to Cancel Changes using button

When I open my form it automatically loads a databases by binding source. There is a button to make changes to the data by values in the textbox There is also a button to cancel those changes( if you have input values into a textbox and press cancel it will revert back to the original data.

I don't know how to code the cancel button, I have successfully done everything else

my current for making changes code (edit:) is:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;


    namespace Eastern_Property_Owners_Club

{

public partial class Club_Record_Maintenance : Form
{
    private OleDbConnection dbConn;     // Connection object
    private OleDbCommand dbCmd;         // Command object

    private string sConnection;
    private string sql;

    public Club_Record_Maintenance()
    {

        InitializeComponent();
        try
        {
            // Construct an object of the OleDbConnection class
            // to store the connection string representing
            // the type of data provider (database) and the source (actual db).

            //string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;Jet OLEDB:Database Password=password";

            //OleDbConnection MyConn = new OleDbConnection(ConnStr);



            // sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Student.mdb";


            sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Eastern_Property_Maintenance.mdb";
            dbConn = new OleDbConnection(sConnection);

            dbConn = new OleDbConnection(sConnection);
            dbConn.Open();

        }
        catch (System.Exception exc)
        {

            MessageBox.Show(exc.Message);
            return;
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //exits and goes to main menu
        MainMenu newForm = new MainMenu();
        newForm.Show();
        this.Close();
    }

    private void Club_Record_Maintenance_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'eastern_Property_MaintenanceDataSet2.Club' table. You can move, or remove it, as needed.
        this.clubTableAdapter.Fill(this.eastern_Property_MaintenanceDataSet2.Club);

    }

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            //declaring value to textboxes
            string Name = txtName.Text;
            string Address = txtAddress.Text;
            string Phone = txtPhone.Text;

            //declaring parameters in the SQL code
            //Field[Name] = whatever is in txtName.Text and so on
            //-----
            //adds values from the textboxes into the database
            sql = " UPDATE Club SET "
                    + " CompanyName = @Name"
                    + ","
                    + " CompanyAddress = @Address"
                    + ","
                    + " CompanyPhone = @Phone"
                    + " WHERE CompanyName = @Name; ";
            dbCmd = new OleDbCommand(sql, dbConn);

            //adding the perameters to ever place-holder in my sql

            dbCmd.Parameters.AddWithValue("@Name", txtName.Text);
            dbCmd.Parameters.AddWithValue("@Address", txtAddress.Text);
            dbCmd.Parameters.AddWithValue("@Phone", txtPhone.Text);

            // Execute query
            dbCmd.ExecuteNonQuery();
        }
        catch (System.Exception exc)
        {
            MessageBox.Show(exc.Message);
            return;
        }
        //a message to show the button has worked
        MessageBox.Show("Club has been Changed! \nRefresh the content by going back to the Main Menu");
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {

        }
       }
    }

it works fine but I don't know how to code a cancel button.

Upvotes: 0

Views: 1819

Answers (1)

Syed Farjad Zia Zaidi
Syed Farjad Zia Zaidi

Reputation: 3360

Make three more global variables and a bool initialized to false to store the values from the database when the data is retrieved check the bool in your data retrieval method as follows and store the values.

if (!oldValues)
{
    // your code to store values from the database when the data is retrieved for the first time
    // change oldValues to true
}

so when the changes are made and stored in the database, those global variables contain your old values. User presses cancel you use those variables to undo changes using the update query as you have used in update but now on the cancel button click event handler... You will also have to change your bool to false again, this way when the data is retrieved again it will store the values in the variables as old values.

Edit:

Do this in your Form Load Event:

bool oldValues = false; // that means there are not old values stored

Make these Global Variables:

string oldName;
string oldAddress; 
string oldPhone;

Now add a button named Add Record Button and do this when it is clicked:

if (!oldValues) //Checks that there are no old Values stored already
{
    if (txtName.Text != "") // handles the case if you have allowed nulls in this field
    {
        oldName = txtName.Text;
    }
    if (txtAddress.Text != "") // handles the case if you have allowed nulls in this field
    {
        oldAddress = txtAddress.Text;
    }
    if (txtPhone.Text != "") // handles the case if you have allowed nulls in this field
    {
        oldPhone = txtPhone.Text;
    }
    oldValues = true; // change to true so that program knows that there are old values stored
    txtName.Clear(); // Clear all text boxes on form so that user can enter new data
    txtAddress.Clear();
    txtPhone.Clear();
}

Do this on your Cancel button:

if (oldValues) // Check if old values are stored
{
    if (oldName != "") // check if its not an empty string
    {
        txtName.Text = oldName;
    }
    else // if it is a empty string then Clear the text box
        txtName.Clear();
    if (oldAddress != "")
    {
         txtAddress.Text = oldAddress;
    }
    else
        txtAddress.Clear();
    if (oldPhone != "")
    {
        txtPhone.Text = oldPhone;
    }
    else
        txtPhone.Clear();
    oldValues = false; // change the oldValues flag to false so that old values can be stored again...
}    

Upvotes: 1

Related Questions