How can i make this work?

Posts   
 
    
LLBLGen
User
Posts: 43
Joined: 10-Apr-2006
# Posted on: 11-Nov-2006 23:31:08   

I have an address Entity which is binded to WinForm controls(Textbox for street, city zip and dropdown for state)

When user wants to enter NEW address, i want the state combo box to show the default state, so for that i have done something like this...

AddressEntity Address =new AddressEntity () ... ... StateComboBox.DataBindings.Add("Value",Address,"State"); StateComboBox.Value="TX";

i also want to save it automatically if the Address is dirty ( New or existing). so i can't do Address ="TX"; because in my save statement i first check to see if the Address is dirty. and if i do Address ="TX" then Address is always dirty even if the user may not have entered the Address.

it works fine BUT as soon as AddressEntity's property changes (when user starts entering data) the state gets lost (disappears), since the NewAddress's Sate is "". How can i still show the state without setting it to an entity even when user is entering data like street, or city or...zip, how can i make this work. Thanks

i am using Adapter, LLBL 2.0 (Oct 23rd), .NET 2.0, SQL 2005

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 12-Nov-2006 19:00:54   

Could you post the code you are using? I can imagine what's going on but it's better to see. simple_smile

LLBLGen
User
Posts: 43
Joined: 10-Apr-2006
# Posted on: 13-Nov-2006 01:03:44   

Here is the code. Basically I have a button that lets the user enter new or save the existing address. as soon as the the user enters something on Street1Text and tabs to next texbox the sate disapears. i want to be able to show that default state on StateComboBox. Thanks

    //This is the Address Entity

    private AddressEntity Address;

   //Action Button's text calls either to save or bind the address

    private void ActionButton_Click(object sender, EventArgs e)
    {
        switch (ActionButton.Text)
        {
            case "Add New":
                BindAddress(null);
                break;
            case "Save":
                SaveAddress();
                break;
        }
    }


    private void BindAddress(AddressEntity Address)
    {
        RemoveAddressBindings();
        if (AddressIsBinded == false)
        {
            // create new AddressEntity if the AddressEntity is null
            if (Address == null)
            {
                Address = new AddressEntity();
            }

            Street1Text.DataBindings.Add("Text", this.Address, "Street1");
            Street2Text.DataBindings.Add("Text", this.Address, "Street2");
            CityText.DataBindings.Add("Text", this.Address, "City");
            StateComboBox.DataBindings.Add("Value", this.Address, "State", true);
            ZipText.DataBindings.Add("Text", this.Address, "ZipCode", true);

            AddressIsBinded = true;

            PopulateState();
            Address.PropertyChanged += new PropertyChangedEventHandler(CurrentAddress_PropertyChanged);
        }
    }

    private void RemoveAddressBindings()
    {
        if (AddressIsBinded == true)
        {
            Street1Text.DataBindings.RemoveAt(0);
            Street2Text.DataBindings.RemoveAt(0);
            CityText.DataBindings.RemoveAt(0);
            StateComboBox.DataBindings.RemoveAt(0);
            ZipText.DataBindings.RemoveAt(0);
            AddressIsBinded = false;
        }
    }

    private void PopulateState()
    {
       StateComboBox.DisplayMember = "StateCode";
       StateComboBox.ValueMember = "StateCode";
       StateComboBox.DataSource = au.GetStateCollection();

       if(Address.isNew)
       {
          StateComboBox.value="TX"; //--->> this is the default state
       } 
     }

     private void  SaveAddress()
     {
       //
       ..some validation...

       //Save the Address if it is dirty
       if (Address.IsDirty)
        {
          ........save address
        }
     }

    private void CurrentAddress_PropertyChanged(object sender, EventArgs e)
    { 
        //Change the text to save so that user can click to save
        ActionButton.Text = "Save";
    }
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39797
Joined: 17-Aug-2003
# Posted on: 13-Nov-2006 09:57:45   

So i.o.w.: if the user doesn't set a state, it should be TX, but if nothing has been changed, it shouldn't be the only thing changed, so it should only be defaulted to TX if the entity gets saved, correct?

Frans Bouma | Lead developer LLBLGen Pro
LLBLGen
User
Posts: 43
Joined: 10-Apr-2006
# Posted on: 13-Nov-2006 23:27:07   

Yes, I guess i could make it work by setting the Address =TX; and then in the save method

If(Address.IsDirty) { and if the street1 is also dirty then save... } but there must be a better way.

Also Fran, while i have you here....is there anyway to know the old value of a filed of an entity before it gets saved?

for example lets say..

I have an Address Entity and the State is TX. i want to save the address only if the state changed from TX to AZ, if not i dont want to save it.

how can i get the old value (TX) without going to the DB and fetching it if the state was binded to a control (TextBox or Combo).

and by the way...so there is no solution to throw an exception in PropertyChanged event ? http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7822&HighLight=1

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 14-Nov-2006 00:17:16   

LLBLGen wrote:

Also Fran, while i have you here....is there anyway to know the old value of a filed of an entity before it gets saved?

for example lets say..

I have an Address Entity and the State is TX. i want to save the address only if the state changed from TX to AZ, if not i dont want to save it.

yourAddressEntity.Fields(AddressFields.State_ID).DBValue

or something like that... simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14987
Joined: 21-Aug-2005
# Posted on: 14-Nov-2006 07:16:58   

Original value -> yourEntityField.DBValue New value -> yourEntityField.CurrentValue