ASP.NET(C#) Storing changes to a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    ASP.NET(C#) Storing changes to a database

    Hi folks,

    Although I'm an experienced programmer, I'm new to ASP and relatively new to web programming, so I hope you'll be patient with my questions.

    I'm working on a project using ASP.NET and C# for the server side code. I've written code to retrieve data from a database and put it into a dynamic html table for the user to view and edit. Works great. Now I want to save those changes and that's where I'm unsure how best to proceed.

    I have a "Save Changes" button on the page that I want the user to use to indicate that they've finished making changes to the data and to store it back to the database. My initial intention is to have the click of this button call my server side C# code and it will scan the table, find what changed (if possible) and store it. If it can't necessarily tell what changed, that's okay as I can store all records regardless as it's only about 200 records.

    First of all, is this really the way to do it? Or is there a smarter way to store changes back to the database?

    Assuming my design is adequate, how is my server side code able to get to the records in this table on the client?

    Also, I'm thinking of storing a hidden "dirty bit" on the client side for each row that changes (via JavaScript) and the "Save Changes" will scan through the table for those that have the dirty bit set and only save those. Is this feasible? Again, is there a smarter way to handle this?

    Thanks for any advice anyone can give. I'm learning a lot about the limitations of web programming and the tricks of having to pass information between client and server. It makes things a bit more challenging, to be sure.

    Robert Porter
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Instead of using an html table I would recommend using a datagrid (or Gridview if you are using .NET 2.0 or higher). This control gives you the ability to identify the row being updated in the ItemCommand event (RowCommand if using GridView).

    Nathan

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      The GridView generates a dynamic html table for you. You can just give it an instance of the DataTable (or DataSet) object and it will populate for you.
      A bonus of using the DataTable is that each row in there has a .RowState property that will tell you if anything has changed (you can then accept or reject changes). Which can be used to writeback data to database.

      Comment

      • RobertTheProgrammer
        New Member
        • Aug 2007
        • 58

        #4
        Thanks for the advice. I'd already spent a lot of time setting up the html table and it works, so I was hoping to avoid having to scrap that and do the GridView which I've just discovered. But the GridView certainly does seem the way to go. I've been experimenting with it (and having a few issues with my database connection, but that's another topic).

        Is it possible to add a dynamically populated DropDownList in a GridView? I'd like to get a list of valid values from the database and add it to the GridView's DropDownList so the user has to choose one of those values.

        Robert

        Comment

        • nateraaaa
          Recognized Expert Contributor
          • May 2007
          • 664

          #5
          Yes you would use a TemplateColumn and place your drop down list in the TemplateColumn.

          Here is a link to a tutorial that explains Gridview and common ways to utilize it.
          Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.


          Nathan

          Comment

          Working...