CodeSnip: Editing Access Databases with ASP.NET 2.0
page 1 of 2
Published: 30 Sep 2004
Unedited - Community Contributed
Creating an ASP.NET 2.0 web form that you can use to edit an Access database can be done without writing any code. Simply drop two web server controls into your web form and set a few properties, and you're done. Wow! That's rapid development.
by Andrew Mooney
Average Rating: 
Views (Total / Last 10 Days): 19792/ 26

Editing and Deleting Rows in an Access Table

[ Download Code ]
First, we are going to need an Access database table that defines at least one primary key. The primary key is needed for making changes to the table. If you were just going to display the database table, you would not need the primary key. Listing 1 shows the database table schema for a list of authors.

Second, we need to create a new web form. For this I used Visual Web Developer 2005 Express Edition.  Now, drag and drop an AccessDataSource control and a GridView control onto your web form. These two controls are both new in version 2.0 of the .NET Framework, and I know they will save developers a lot of time. Now we just need to set a few properties.

Set the following properties on the AccessDataSource control:

  1. DataFile: "data\pubs.mdb"
  2. SelectCommand: "select * from [authors]"
  3. DeleteCommand: "DELETE FROM [authors] WHERE [au_id] = ?"
  4. UpdateCommand: "UPDATE [authors] SET [au_lname] = ?, [au_fname] = ?, [phone] = ?, [address] = ?, [city] = ?, [state] = ?, [zip] = ?, [contract] = ? WHERE [au_id] = ?"

Set the following properties on the GridView control:

  1. AllowPaging: True (Turns on paging, the default page size is 10)
  2. AllowSorting: True (Turns on column sorting)
  3. AutoGenerateEditButton: True (Allows you to edit rows)
  4. AutoGenerateDeleteButton: True (Allows you to delete rows)
  5. DataKeyNames: au_id (Primary key required for making changes)
  6. DataSourceID: DataSetDataSource1 (Reference to the DataSource control)

That's all there is!  You now have a web form that can edit and delete rows in an Access database table, all of this without writing a single line of code. Listing 2 shows the web form.  Notice the absence of code. Screen Shot 1 shows the web form in action.

There is only one piece missing--the ability to add rows to our Access table. The only problem is that GridView will not allow you to add rows. Not to worry, we can still accomplish this without writing code. Let's see how.

View Entire Article

User Comments

No comments posted yet.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-19 2:01:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search