[ 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:
- DataFile: "data\pubs.mdb"
- SelectCommand: "select * from [authors]"
- DeleteCommand: "DELETE FROM [authors] WHERE [au_id] = ?"
- 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:
- AllowPaging: True (Turns on paging, the default page size is 10)
- AllowSorting: True (Turns on column sorting)
- AutoGenerateEditButton: True (Allows you to edit rows)
- AutoGenerateDeleteButton: True (Allows you to delete rows)
- DataKeyNames: au_id (Primary key required for making changes)
- 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.