Updating Records in a Microsoft Access Database with ASP.NET

The following tutorial follows on from Reading records from an Access Database and Inserting Records into an Access Database.

We’ll not go into a detailed explanation of importing the System.Data.Oledb Namespace, creating the connection object and the command object and passing parameters into these objects as we’ve covered these extensively in the previous two parts.

The only difference in our update code than our insert code is the SQL statement that we pass in as a parameter to our command object.

As we’re just doing a straightforward update and not returning any records we don’t need a DataReader object which we did in the Reading records example. We can simply execute the command, and to do this we can use the ExecuteNonQuery method of our Command object.

We’ve added a label ‘label1’ control to our page and set it’s text property to ‘Record Updated’ once the code has been run.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.Oledb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim Connection As OledbConnection
       Connection = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;" & _
                                     "Data Source=C:\Inetpub\wwwroot\code\Customers.mdb")
       Connection.Open()
       Dim Command As OleDbCommand
       Command=New OleDbCommand("Update tblCustomers Set FirstName='Mic' WHERE LastName= 'Wall')", Connection)
       Command.ExecuteNonQuery()       
       Connection.Close()

       Label1.Text="Record Updated"
              
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Update Record(s) in MS Access Database</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label ID=label1 runat="server" />
    </div>
    </form>
</body>
</html>


Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Get your best asp web hosting provider now and save 25%