ASP.NET - Update database with databinding


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: ASP.NET - Update database with databinding

  1. #1
    MichaelChoi Guest

    ASP.NET - Update database with databinding


    I have been a VB progammer for 10 years. I spent most of my time in creating
    data entry forms and data editing forms. That is my main focus is in retrieving
    and saving data into the database.

    I've been doing the job without using databinding. Now, since Microsoft has
    emphasize so much on databinding, so, recently I've tried some test on databinding
    on a web form.

    What I find out is, it is easy to display data on a web form using databinding;
    but it is hard or impossible to save changes back into the database by databinding.
    I have to re-populate the datatable in the dataset with changes before updating
    which is actually not databinding.

    To elaborate the case, lets say I have a button(btn1),textbox(txt1), an sqldataadapter(da1)
    and a dataset(ds1) filled by ds1 and txt1.text is binded to table(0).columns(0)
    in ds1.
    After the web form is loaded, txt1 shows data as expected. Good! But, I also
    expect after editing txt1, click btn1 and write a very simple short piece
    of code, the database will be updated with the changes.
    The code I wrote in btn1_click event handler is
    Dim scb As New SqlClient.SqlCommandBuilder(da1)
    da1.update(ds1)

    But it didn't work!!!
    The reason is the Page is POSTED BACK before running the btn1_Click event
    handler. So, the textbox is re-populated with the original data from the
    database. The whole issue lies in Microsoft persists textboxes but does not
    persist dataset and dataadapter when post back and there is no way to save
    them into ViewState before the Page_Load event.

    I've tried a number of ways but still couldn't find a way to make use of
    databinding to update the database with changes. I've to write code to update
    table(0) in ds1 (like, ds1.table(0).rows(0)(1)=txt1.text.) before calling
    da1.update(ds1). If I have 20 fields, I would have to write 20 such lines
    which is not what I expect.

    So, I would be very grateful if someone can point out to me a proper way
    to make use of the databinding to facilitate database update.

    Thanking in anticipation.

    Michael



  2. #2
    Michael Guest

    Re: ASP.NET - Update database with databinding


    Do you have you dataset creation and databinding embeded in a IsPostBack
    condition in the page load event...?

    if Not IsPostBack then
    'init dataset and do data binding
    ...
    end if




    "MichaelChoi" <michaelc@sah.org.au> wrote:
    >
    >I have been a VB progammer for 10 years. I spent most of my time in creating
    >data entry forms and data editing forms. That is my main focus is in retrieving
    >and saving data into the database.
    >
    >I've been doing the job without using databinding. Now, since Microsoft

    has
    >emphasize so much on databinding, so, recently I've tried some test on databinding
    >on a web form.
    >
    >What I find out is, it is easy to display data on a web form using databinding;
    >but it is hard or impossible to save changes back into the database by databinding.
    >I have to re-populate the datatable in the dataset with changes before updating
    >which is actually not databinding.
    >
    >To elaborate the case, lets say I have a button(btn1),textbox(txt1), an

    sqldataadapter(da1)
    >and a dataset(ds1) filled by ds1 and txt1.text is binded to table(0).columns(0)
    >in ds1.
    >After the web form is loaded, txt1 shows data as expected. Good! But, I

    also
    >expect after editing txt1, click btn1 and write a very simple short piece
    >of code, the database will be updated with the changes.
    >The code I wrote in btn1_click event handler is
    > Dim scb As New SqlClient.SqlCommandBuilder(da1)
    > da1.update(ds1)
    >
    >But it didn't work!!!
    >The reason is the Page is POSTED BACK before running the btn1_Click event
    >handler. So, the textbox is re-populated with the original data from the
    >database. The whole issue lies in Microsoft persists textboxes but does

    not
    >persist dataset and dataadapter when post back and there is no way to save
    >them into ViewState before the Page_Load event.
    >
    >I've tried a number of ways but still couldn't find a way to make use of
    >databinding to update the database with changes. I've to write code to update
    >table(0) in ds1 (like, ds1.table(0).rows(0)(1)=txt1.text.) before calling
    >da1.update(ds1). If I have 20 fields, I would have to write 20 such lines
    >which is not what I expect.
    >
    >So, I would be very grateful if someone can point out to me a proper way
    >to make use of the databinding to facilitate database update.
    >
    >Thanking in anticipation.
    >
    >Michael
    >
    >



  3. #3
    Michael Choi Guest

    Re: ASP.NET - Update database with databinding


    I do.
    But the point is, the page is posted back before the button_Click event,
    so, once binded the textbox will be re-populated with data from database.

    Of course, I can save data into the database by executing an insert SQl.
    But what I'm after is a simple way making use of databinding.

    "Michael" <modeen@acitel.com> wrote:
    >
    >Do you have you dataset creation and databinding embeded in a IsPostBack
    >condition in the page load event...?
    >
    >if Not IsPostBack then
    > 'init dataset and do data binding
    > ...
    >end if
    >
    >
    >
    >
    >"MichaelChoi" <michaelc@sah.org.au> wrote:
    >>
    >>I have been a VB progammer for 10 years. I spent most of my time in creating
    >>data entry forms and data editing forms. That is my main focus is in retrieving
    >>and saving data into the database.
    >>
    >>I've been doing the job without using databinding. Now, since Microsoft

    >has
    >>emphasize so much on databinding, so, recently I've tried some test on

    databinding
    >>on a web form.
    >>
    >>What I find out is, it is easy to display data on a web form using databinding;
    >>but it is hard or impossible to save changes back into the database by

    databinding.
    >>I have to re-populate the datatable in the dataset with changes before

    updating
    >>which is actually not databinding.
    >>
    >>To elaborate the case, lets say I have a button(btn1),textbox(txt1), an

    >sqldataadapter(da1)
    >>and a dataset(ds1) filled by ds1 and txt1.text is binded to table(0).columns(0)
    >>in ds1.
    >>After the web form is loaded, txt1 shows data as expected. Good! But, I

    >also
    >>expect after editing txt1, click btn1 and write a very simple short piece
    >>of code, the database will be updated with the changes.
    >>The code I wrote in btn1_click event handler is
    >> Dim scb As New SqlClient.SqlCommandBuilder(da1)
    >> da1.update(ds1)
    >>
    >>But it didn't work!!!
    >>The reason is the Page is POSTED BACK before running the btn1_Click event
    >>handler. So, the textbox is re-populated with the original data from the
    >>database. The whole issue lies in Microsoft persists textboxes but does

    >not
    >>persist dataset and dataadapter when post back and there is no way to save
    >>them into ViewState before the Page_Load event.
    >>
    >>I've tried a number of ways but still couldn't find a way to make use of
    >>databinding to update the database with changes. I've to write code to

    update
    >>table(0) in ds1 (like, ds1.table(0).rows(0)(1)=txt1.text.) before calling
    >>da1.update(ds1). If I have 20 fields, I would have to write 20 such lines
    >>which is not what I expect.
    >>
    >>So, I would be very grateful if someone can point out to me a proper way
    >>to make use of the databinding to facilitate database update.
    >>
    >>Thanking in anticipation.
    >>
    >>Michael
    >>
    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center