1

I'm new to CSLA , so here is my question. I have simple windows form application with datagridview and 2 buttons, Save and Delete.I need a solution for implementing Save() method for Save button, and Delete() method for Delete button.I believe it is really easy , but I can't figure out how to call these 2 methods.Datagridview control is editable, so my idea is when user add new row or edit some of existing cells and click on button save ,those data will be recorded in my database. Thank you in advance!

I'm using root editable collection and child editable stereotype. Here is a code for root:

[Serializable]
public class PDVCollection : BusinessBindingListBase<PDVCollection,PDV>
{
    private PDVCollection()
    {
        AllowNew = true;
    }
    protected override object AddNewCore()
    {
        var item = PDV.PDV();
        Add(item);
        return item;
    }

    #region Factory Methods


    public static PDVCollection GetAll()
    {
        return DataPortal.Fetch<PDVCollection>();
    }

    protected override void DataPortal_Update()
    {
        Child_Update();
    }

    #endregion

    #region Data Access
    private void DataPortal_Fetch()
    {
        RaiseListChangedEvents = false;

        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVSelect", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        try
        {
            con.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                var stopa = PDV.GetPDV(dr);
                Add(stopa);
            }
            con.Close();
        }
        catch (Exception xcp)
        {
            throw xcp;
        }

        RaiseListChangedEvents = true;
    }

}
    #endregion

and here is a code for editable child

[Serializable]
public class PDV : BusinessBase<PDV>
{
    public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
    public int Id
    {
        get { return GetProperty(IdProperty); }
        private set { LoadProperty(IdProperty, value); }
    }

    public static readonly PropertyInfo<Guid> UidProperty = RegisterProperty<Guid>(c => c.Uid);
    public Guid Uid
    {
        get { return GetProperty(UidProperty); }
        private set { LoadProperty(UidProperty, value); }
    }

    public static readonly PropertyInfo<decimal> StopaProperty = RegisterProperty<decimal>(c => c.Stopa);
    public decimal Stopa
    {
        get { return GetProperty(StopaProperty); }
        set { SetProperty(StopaProperty, value); }
    }


    #region Factory Methods

    internal static PDV NewPDV()
    {
        return DataPortal.CreateChild<PDV>();
    }

    internal static PDV GetPDV(MySqlDataReader dr)
    {
        return DataPortal.FetchChild<StopaPDV>(dr);
    }

    private StopaPDV()
    {

    }

    #endregion



    #region DataAccess

    protected override void Child_Create()
    {
        LoadProperty(UidProperty, Guid.NewGuid());
        base.Child_Create();
    }

    private void Child_Fetch(MySqlDataReader dr)
    {
        LoadProperty(IdProperty,Int32.Parse(dr[0].ToString()));
        LoadProperty(UidProperty, Guid.Parse(dr[1].ToString()));
        LoadProperty(StopaProperty, Decimal.Parse(dr[2].ToString()));

    }

    private void Child_Insert()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVInsert", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_uid", MySqlDbType.VarChar, 36).Value = Uid;
        cmd.Parameters.Add("_stopa", MySqlDbType.Decimal).Value = Stopa;
        cmd.Parameters.Add("_id", MySqlDbType.Int32).Direction = System.Data.ParameterDirection.Output;
        int ID = 0;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            ID =  Convert.ToInt32(cmd.Parameters["_id"].Value);
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }



    }

    private void Child_Update()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVUpdate", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_stopa",MySqlDbType.Decimal).Value = Stopa;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }
    }

    private void Child_DeleteSelf()
    {
        MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=1234;persistsecurityinfo=True;database=dbpos");
        MySqlCommand cmd = new MySqlCommand("usp_PDVDeleteById", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("_id", MySqlDbType.Int32).Value = Id;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception xcp)
        {

            throw xcp;
        }
    }
    #endregion
}
Hank Mooody
  • 527
  • 11
  • 29

1 Answers1

2

If I understand you correctly, you are looking for help on how to implement the Save and Delete button event handlers that will act upon your CSLA Editable Root List/Collection to effect a save or delete of the data in the business objects.

Assuming this is indeed what you are after, then the following should help.

Save

In your Save button event handler, you would simply call the Save() instance method on your PDVCollection instance.

Delete

As for the Delete button event handler, you would need to know which item in the collection you want to delete and then remove that from the parent/root collection.

Code

Below is a quick example of the code behind for the Form (with the 2 x Buttons, a DataGridView and a BindingSource control):

public partial class MainForm : Form
{
    private BusinessLogic.PDVCollection _pdvColllection;
    private BusinessLogic.PDV _pdvCurrentlySelected;

    public MainForm()
    {
        InitializeComponent();

        // Keep a reference to the editable root collection
        _pdvColllection = BusinessLogic.PDVCollection.GetList();

        // Initialise the BindingSource with data. The DataGridView is connected to the BindingSource
        pdvCollectionBindingSource.DataSource = _pdvColllection;
    }

    private void pdvCollectionBindingSource_CurrentChanged( object sender, EventArgs e )
    {
        // Update a local reference with the currently selected item in the binding source (set by the DataGridView)
        _pdvCurrentlySelected = pdvCollectionBindingSource.Current as BusinessLogic.PDV;
    }

    private void saveButton_Click( object sender, EventArgs e )
    {
        // Save the root collection - this will update all of the items in the collection.
        // - Note a new root collection is returned.
        if ( !_pdvColllection.IsValid )
        {
            MessageBox.Show( "Cannot save list because some items are invalid", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information );
            return;
        }

        _pdvColllection = _pdvColllection.Save();

        // Update the binding source with the new instance;
        pdvCollectionBindingSource.DataSource = null;
        pdvCollectionBindingSource.DataSource = _pdvColllection;
    }

    private void deleteButton_Click( object sender, EventArgs e )
    {
        // Delete requires a PDV instance to be selected in the DataGridView
        if ( _pdvCurrentlySelected == null )
        {
            MessageBox.Show( "Item to delete not selected", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information );
            return;
        }

        _pdvColllection.Remove( _pdvCurrentlySelected );

        // Depending on whether you want to save this delete to the background immediately, or leave it up to the user you may want to 
        // Save the root collection next:
        saveButton.PerformClick();
    }
}

Hope that helps.

Community
  • 1
  • 1
Jaans
  • 4,598
  • 4
  • 39
  • 49