CodeSnip: Editing Access Databases with ASP.NET 2.0
page 2 of 2
by Andrew Mooney
Feedback
Average Rating: 
Views (Total / Last 10 Days): 19764/ 33

Adding Rows to an Access Table

[ Download Code ]
The GridView control will not allow you to add new rows to a data source, but the DetailsView (another new control in ASP.NET 2.0) will. While the GridView can display multiple rows, the DetailsView control displays only one row at a time, but the DetailsView will allow you to edit, delete, and add rows to a data source.

Using the same web form, drag and drop a second AccessDataSource control and a DetailsView control onto the web form. Now we just set a few properties. You need a second AccessDataSource control because you are going to set a filter expression on it so that it only displays one row (the row you select). Microsoft recommends using a separate DataSource control with each data control even if you are not using a filter.

Set the properties of the second AccessDataSource control are the same as the first with one exception and three changes:

  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] = ?"
  5. InsertCommand: "INSERT INTO [authors] ([au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
  6. FilterExpression: au_id='@auid' (Make sure to include single quotes)
  7. FilterParameters (Add Parameter)
    • Parameter source: Control
    • ControlID: GridView1
    • Name: auid

Set the properties of the GridView control:

  1. AutoGenerateEditButton: False
  2. AutoGenerateDeleteButton: False
  3. AutoGenerateSelectButton: True

Set the properties of the DetailsView control:

  1. AutoGenerateEditButton: True
  2. AutoGenerateDeleteButton: True
  3. AutoGenerateInsertButton: True

Save your web form and place the database file in the "data" sub-directory of the same virtual directory. Remember this code sample requires ASP.NET 2.0. Using four web server controls we now have a web page that will edit, delete, and add rows to an Access database table. All of this in just a few minutes. Wow! When you click the select button on the GridView, that row will be displayed in the DetailsView. You can then edit or delete that row. You can also add a new row from the DetailsView.

Screen Shot 2  shows the revised web form in action. Listing 3 shows the web form.  Don't panic!  It contains a small amount of code.  The code is not necessary for the web form to function, but it sure adds a nice feature. When you make changes in the DetailsView they are not updated in the GridView. To fix this we just bind the GridView to the database table every time we make a change.  I'm sorry, but this requires a small amount of code.

In Source view add the following properties to the DetailsView:

  1. onitemupdated="DetailsView1_ItemUpdated"
  2. onitemInserted="DetailsView1_ItemInserted"
  3. onitemdeleted="DetailsView1_ItemDeleted"

Then add the sub routines as show in Listing 3. In each we simply bind the database table to the GridView so that it reflects the changes made in the DetailsView. The code is not required, but without this code, you would have to sort or change pages in the GridView to see the changes.

In conclusion, let me say that the new controls in ASP.NET 2.0 are going to make editing Access databases much easier.  I also look forward to using the rest of these new controls to rapidly develop web applications.


View Entire Article

User Comments

No comments posted yet.






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:33:28 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search