O'Reilly logo

ASP.NET in a Nutshell by Matthew MacDonald, G. Andrew Duthie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Inserting and Updating Data

Reading and binding data is all very well, but for most applications, it’s only part of what the application needs to do. Another important feature is the ability to insert new rows and/or update existing rows of data. As with reading data, the DataSet and SqlDataAdapter (or OleDbDataAdapter ) classes come in handy. Another class that is extremely useful is the SqlCommandBuilder (or OleDbCommandBuilder) class, which is discussed later in this section.

Example 7-6, while more complicated than previous examples, adds a relatively small amount of code to support adding and updating rows to the Pubs Titles table.

Example 7-6. InsertUpdateTitles.aspx

<%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <title>Insert/Update Example</title> <script runat="server"> Dim Titles As New DataSet( ) Dim TitlesAdpt As New SqlDataAdapter( ) Sub Page_Load(Sender As Object, e As EventArgs) If Not IsPostBack Then GetTitleData("") BindGrid( ) End If End Sub Sub Add_Click(Sender As Object, e As EventArgs) Page.RegisterHiddenField("EditMode", "Add") title_id.ReadOnly = False Display.Visible = False InsertUpdate.Visible = True End Sub Sub Cancel_Click(Sender As Object, e As EventArgs) Response.Redirect("InsertUpdateTitles.aspx") End Sub Sub Edit_Click(sender As Object, e As DataGridCommandEventArgs) GetTitleData("WHERE title_id = '" & e.Item.Cells(1).Text & "'") title_id.Text = Titles.Tables(0).Rows(0)(0) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required