Results 1 to 2 of 2

Thread: Data Set

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2003
    Posts
    5

    Data Set

    Enclosed is code from a 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">
    <aspataGrid 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" />
    </aspataGrid>
    </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>

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    One, is it possible to insert multiple records into the dataset before updating the dataset? If so how?

    of course just use the newrow and addrow method, obviously you need to dim your datarow first, stuck for code just ask


    Two, is it possible after inserting multiple records, while still not updating the data store, load a datagrid from the dataset?

    mmm. yes look up merge dataset

    Three, Why did the author use a temporary dataset in this example?

    probably for the same reaon as I do, bind a dataset to a datagrid which is effectively data entry only and you wish to throw away (i.e. never update), the dataset just acts as a lead to create records in other datasets. For the really lazy programmer for your target dataset update, use a select command that produces no records, then you can use the datadapter update command to generate your sql command for you - beware of deletes!!!

    Richard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width