none
TextBox databinding update not working RRS feed

  • Question

  • Im trying to make textbox push changes back to db but it does not work.

     

    I have a binding source populated like this

    CRMCustomerBindingSource.DataSource = BokningarDataContext.GetCustomer(5565750626);

     

     

    what do i need to do to make it update the source when the textbox loses focus? It just wont work Sad

     

     

    Many Thanks

    Saturday, December 6, 2008 8:18 AM

All replies

  • Hi

     

    you just need to set (Databindings - Text ) property of TextBox with CRMCustomerBindingSource- ***(Code or FirstName , .....)

     

    when you use binding source and bind properties of TextBoxes or any control to the bindingsource, after you make any change in text of textbox  ,authomatically this change back to the source and need nothing more.

    and for updating this data in the database you jaust need to call SubmitChanges() method.

    Saturday, December 6, 2008 9:43 AM
    Answerer
  • Thank you for your reply!

     

    I have, like you said bind my controls to both the Tag property and the Text property of the control. But i have no "SubmitChanges" method in the instance of BindingSource class (CRMCustomerBindingSource). Where should it be? im running Visual Studio 2008 and does not use datareader or dataset.

     

    The GetCustomer(long) is returning a TableClass called CRMCustomer made automaticly by Visual studio when i created the linq classes.

     

    its declaration is like this:

     

    Code Snippet

    public static CRMCustomer GetCustomer(long organsiationsnummer)

    {

    try

    {

    BokningarDataContext db = new BokningarDataContext();

    CRMCustomer cust = (from c in db.CRMCustomers where

    c.Organisationsnr == organsiationsnummer select c).SingleOrDefault<CRMCustomer>();

     

    return cust;

    }

    catch (System.Data.SqlClient.SqlException ex)

    {

    throw ex;

    }

    }

     

     

    could it have something to do with the fact that the GetCustomer is declared static?

     

    Thanks!

    Saturday, December 6, 2008 6:30 PM
  • HI

     

    SubmitChanges() is a method of BokningarDataContext. with calling this method you can make your database update.

    and also for fetching one record from database you can use method Syntax query with  Lambada expression:

     

    BokningarDataContext db = new BokningarDataContext();

    CRMCustomer cust = db.CRMCustomers.Single(c => c.Organisationsnr == organsiationsnummer );

     

    Sunday, December 7, 2008 5:00 AM
    Answerer
  •  Hi

     

    Yes there is a method called the BokningarDataContext. The trouble is im not using the datacontext. Im using BindingSource class to bind the TextBoxes. So the only time i use the BokningarDataContext the first time when i fetch the record to the BindingSource. Witch is done in GetCRMCustomer function, wich is returns a CRMCustomer table.

     

    Is it any way to update the database without using the BokningarDataContext?

     

    Sunday, December 7, 2008 8:25 AM
  • I don't understand what you mean, you can always create a new datacontext instance, retrieve your modified objects from the form, update their values and submit

    otherwise i guess you could execute some normal sql code or call a storedprocedure but i wouldn't see any advantage here
    Monday, December 8, 2008 1:50 PM
  • Sorry for my late reply, i have been on vacation :)

    You´re right, i can create an instance for the control when it loses focus. But if i design my app like that i would have to code an "LostFocus" event for every textbox that has updateable content (about 30 or 40), thats not a good design. Ive read somewhere that the textboxes, when they are updated can push the changes back to the db with very little coding. Hows that done?

    Friday, January 9, 2009 10:13 PM