Databind to local variables

Hello all

Platform: VS 2005 Visual C#

i am trying to create a databind from db value to local variable, menaing that the local variable will hold the db data.
But, i've come to noticed that only GUI controls Properties can be bind, it looks like that the binding occurs on paint or something like that.
Is there away to bind data from db to local variables
I've solved this problem by extending text box adding it two more properties and bind them to db, so this is not the solution i am looking for here.

My second wuestion on the same subject:
Can i do a custom databinding that will present two fields from db, for example FirstName + ' ' + LastName (like in asp.net)

Itzik Katzav



Answer this question

Databind to local variables

  • hseb

    Hang on a second...

    ADO.Net uses a disconnected data model, so what you're binding to is a locally-cached copy of the database held in a DataTable.   Changes made to bound controls are NOT immediately reflected in the database.

    OK, with that settled, let's move on...

    The concept of "binding" is tied to Controls.   The only reason they invented "binding" was to allow data from a database to be displayed/manipulated in controls of some sort.   So, if you aren't using a Control (instead you're just using a variable), there no need for binding.

    So, how is it done    Actually it's quiet simple... you just use the '=' (equal sign) to assign the value of the row/column. 

    Let's assume you've already navigated to a particular row that you're interested in... in this example, row 14

    strSomething = DataSet.Tables["Table1"].Rows[14]["FirstName"]

  • Markus Schuhmacher

    hello graye

    well all that you mention i already know but the reason i was looking to do so, is to assure that navigating for example will change the local variable value automatically without me catching the position changed event.

    from your answer i understand that there is no such way to do so, simply because the binding mechanism was created to serve controls, i appriciate your answer.

    Do you have an answer to my second question, How to bind a control to more than one member, for example:

    FirstName + ' ' + LastName without using views

    ibelieve there is no way but i just want to be sure!!!


    Thanks anyway
    Itzik Katzav

  • prabakaranm

    Why do you want to bind values directly to the database   This means that a connection to your database will always be open, and that any change you make to your local variables will immediately be reflected in your database, without any validation, transaction support, etc.

    A better option is to have a function to retrieve all your data into your local variables (or into a dataset, datareader, etc.), and a separate function that saves all your data.  This way you can work with local data, and only save to the database when you need/want to.

    Hope this helps, let me know if you need more details,
    Josh Lindenmuth

  • Databind to local variables