Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 21st, 2003, 09:46 PM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default database question

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>
 
Old July 23rd, 2003, 04:56 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I can guide you Q1 and Q2.
Q1 & Q2
Yes, you can. However, I am not familiar with SqlCommandBuider. Before you create the DataReader. I would do as follows:
Dim com AS SqlCommand 'Global object
com.Connection = objConnection 'set connection
com.Open() 'open the connection
'you can set the loop for multiple insertion
do while
com.CommandText = "insert tbl1 values ('hello', 'how are you')"
com.ExecuteNonQuery() 'you are happy now.
loop
com.Close() 'close connection

Something like above should work. Also, you have to check my syntax, it is not 100% correct. I just gave you the idea.
 
Old July 29th, 2003, 05:58 AM
Registered User
 
Join Date: Jul 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Q1: You can do this as many times as you like by repeating the code in Sub btnInsert_Click. ie:

    Dim r As DataRow
    r = dsTemp.Tables(0).NewRow()
    r("CategoryName") = txtCategory.Text
    r("Description") = txtDescription.Text
    dsTemp.Tables(0).Rows.Add(r)

Q2: Yes, just bind the grid to the DataSet whenever you want.

Q3: Don't assume it is temporary just because it is called 'dsTemp'








Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Question rose77 Access 0 January 9th, 2005 02:14 PM
Database question hlchuah77 SQL Language 5 November 12th, 2004 12:40 PM
Database question! Calibus Classic ASP Databases 3 August 17th, 2004 05:23 PM
HELP database question TnTandyO Classic ASP Databases 0 December 1st, 2003 12:39 PM
database question. kyootepuffy Classic ASP Databases 5 September 29th, 2003 03:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.