Enclosed is code from the book. I have a couple questions.
One, is it possible to insert multiple records into the dataset before updating the dataset? If so how?
Two, is it possible after inserting multiple records, while still not updating the data store, load a datagrid from the dataset?
Three, Why did the author use a temporary dataset in this example?
Thanks again for the help
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataGrid - Insert</title>
</head>
<body leftMargin="0" topMargin="0">
<form id="Form1" method="post" runat="server">
<table id="Table1"
style="Z-INDEX: 110; LEFT: 5px; POSITION: absolute; TOP: 5px"
cellSpacing="0" cellPadding="0" width="300" border="0">
<tr>
<td colSpan="3">
<asp:DataGrid id="dgNorthwind" runat="server"
Width="728" Height="234" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1"
BackColor="White" CellPadding="4" EnableViewState="False">
<ItemStyle ForeColor="#330099" BackColor="White" />
<HeaderStyle Font-Bold="True"
ForeColor="#FFFFCC" BackColor="#990000" />
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC" />
<PagerStyle HorizontalAlign="Center"
ForeColor="#330099" BackColor="#FFFFCC" />
</asp:DataGrid>
</td>
</tr>
<tr>
<td colSpan="3"></td>
</tr>
<tr>
<td colSpan="3">
<p>
<asp:Label id="Label1" runat="server"
Width="317" BackColor="Firebrick" ForeColor="White">
Insert a new record...
</asp:Label>
</p>
</td>
</tr>
<tr>
<td colSpan="3"></td>
</tr>
<tr>
<td colSpan="3">
<asp:Label id="Label2" runat="server" Width="85px">
Category</asp:Label>
<asp:TextBox id="txtCategory" runat="server" Width="220px" />
<asp:RequiredFieldValidator id="rfvCategory" runat="server"
ErrorMessage="Please insert the category name..."
ControlToValidate="txtCategory" />
</td>
</tr>
<tr>
<td colSpan="3">
<asp:Label id="Label3" runat="server" Width="85">
Description</asp:Label>
<asp:TextBox id="txtDescription" runat="server" Width="220" />
</td>
</tr>
<tr>
<td colSpan="3">
<asp:Button id="btnInsert" runat="server" OnClick="btnInsert_Click"
Width="317" Height="22"
BorderColor="#FFFFC0" BorderStyle="Solid"
BackColor="Firebrick" ForeColor="White" Text="Insert" />
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="
VB" runat="server">
Dim objConnection As SqlConnection
Dim daNorthwind As SqlDataAdapter
Dim dsNorthwind As DataSet
Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = ConfigurationSettings.AppSettings("NWind")
objConnection = New SqlConnection(strConnection)
Dim strSQL As String = "SELECT CategoryID, CategoryName, Description " & _
"FROM Categories"
daNorthwind = New SqlDataAdapter(strSQL, objConnection)
' Create a command builder object in order to create
' INSERT, UPDATE, and DELETE SQL statements automatically
Dim cb As New SqlCommandBuilder(daNorthwind)
' Is the page being loaded for the first time?
If Not Page.IsPostBack Then
FillDataGrid()
End If
End Sub
Sub FillDataGrid()
' Create a new dataset to contain categories' records
dsNorthwind = New DataSet()
' Fill the dataset retrieving data from the database
daNorthwind.Fill(dsNorthwind)
' Set the DataSource property of the DataGrid
dgNorthwind.DataSource = dsNorthwind.Tables(0).DefaultView
' Bind the dataset data to the DataGrid
dgNorthwind.DataBind()
End Sub
Sub btnInsert_Click(Sender As Object, E As EventArgs)
' If user has filled every text box correctly...
If Page.IsValid Then
' Create a temporary dataset to contain the new record
Dim dsTemp As New DataSet()
' Fill the temporary dataset
daNorthwind.Fill(dsTemp)
' Create a new row
Dim r As DataRow = dsTemp.Tables(0).NewRow()
' Add the category name, reading its value from the text box
r("CategoryName") = txtCategory.Text
' Add the category description, reading its value from the text box
r("Description") = txtDescription.Text
' Add the new row into the dataset's rows collection
dsTemp.Tables(0).Rows.Add(r)
' Update the database using the temporary dataset
daNorthwind.Update(dsTemp)
' Usually, you have to call the AcceptChanges() method in order to align the
' dataset with records in the database. Because this is a temporary dataset,
' we can omit this instruction.
' dsTemp.AcceptChanges()
' Refresh the data grid to display the new record
FillDataGrid()
End If
End Sub
</script>