Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET 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 March 16th, 2004, 02:22 PM
Authorized User
 
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding rows to datatable

I have a question about adding rows to a datatable. What I want to do is have the user add rows to a datatable on the same page and then bind a datagrid to the datatable showing all the "entries".

What is happening is that I'm successfully adding one row to the datatable and it's binding properly, only that I seem to be recreating the datatable every time the user posts new form data. There are actually two forms on the page (see code).

After the user is done entering all their lines of data, I then want the user to commit all the lines to the database (the datatable mirrors the table in the database) with one other button click.

I'll post some code. I hope I was clear. Thanks very much.

[codebehind]
Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class general_journal_entry
    Inherits System.Web.UI.Page

Public dg_Lines as System.Web.UI.WebControls.DataGrid
Public dt_Lines as New DataTable
Public oA as New Accounting.NumberUtilities.CalculateClass

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        HideDG()
        Dim strTransNumber as String = Request.Form("Trans_Number")

        If dt_Lines.Rows.Count <= 0 Then
            Response.Write("rowcount less than or equal to 0<br/>")
            dt_Lines.Columns.Add("Line_Number")
            dt_Lines.Columns.Add("Description")
        Else
            Response.Write("more than 0 rows<br/>")
        End If

        'code when the user has entered the header information... fetch the trans number and put it in the hidden input.
        If Request.Form("byAccount") = "1" Then
            REsponse.Write("account<br/>")
            InsertAccountNumberLine()
            BindGrid()
        End If

        If Request.Form("byWO") = "1" Then
            REsponse.Write("wo<br/>")
            InsertWorkOrderLine()
            BindGrid()
        End If

    End Sub


    Private Sub InsertAccountNumberLine()
        REsponse.Write("account<br/>")
        Dim oRow as DataRow = dt_Lines.NewRow()
        oRow.Item(0) = (Integer.Parse(dt_Lines.Rows.Count) + 1)
        oRow.Item(1) = "account description for line " & (dt_Lines.Rows.Count + 1).ToString()
        dt_Lines.Rows.Add(oRow)
        ShowDG()
    End Sub

    Private Sub InsertWorkOrderLine()
        Response.Write("wo<br/>")
        Dim oRow as DataRow = dt_Lines.NewRow()
        oRow.Item(0) = (Integer.Parse(dt_Lines.Rows.Count) + 1)
        oRow.Item(1) = "WO description for line " & (dt_Lines.Rows.Count + 1).ToString()
        dt_Lines.Rows.Add(oRow)
        ShowDG()
    End Sub

    Private Sub BindGrid()
     dg_Lines.DataSource=dt_Lines
     dg_Lines.DataBind()
    End Sub

    Private Sub ShowDG()
        dg_Lines.Visible=True
    End Sub

    Private Sub HideDG()
        dg_Lines.Visible=False
    End Sub


End Class
[/codebehind]

[aspx page]
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="general_journal_entry.aspx.vb" Inherits="Accounting.general_journal_entry"%>
<%@ Register TagPrefix="NavigationBar" TagName="Toolbar" src="../navigator.ascx"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>CMWC ACCOUNTING | General Journal Entry</title><LINK rel="stylesheet" type="text/css" href="........style.css">
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name=vs_defaultClientScript content="JavaScript">

    <script language="javascript">
        function showDiv(whichDiv)
        {
            //DivToModify = 'general_journal_entry_' + whichDiv;
            //alert(DivToModify);

            //document.all('div_Form').innerHTML = document.all(DivToModify).innerHTML;
            //document.all(DivToModify).style.visibility='visibl e';
            if(whichDiv==1)
            {
                document.all('general_journal_entry_WO').style.vis ibility='visible';
                document.all('general_journal_entry_ACCOUNT').styl e.visibility='hidden';
            }
            else
            {
                document.all('general_journal_entry_ACCOUNT').styl e.visibility='visible';
                document.all('general_journal_entry_WO').style.vis ibility='hidden';
            }
        }
        function popAccountWindow()
        {
            Win=window.open('....','AccountWin','height=300,wi dth=200,scrollbars=yes,resize=no');
            Win.focus();
        }
    </script>

  </head>
  <body MS_POSITIONING="GridLayout" topmargin="5" leftmargin="5">
<NavigationBar:Toolbar id="Navbar" runat="server" loc="General Journal" main_url="../default.aspx" url="default.aspx"></NavigationBar:Toolbar>

        <form id="frm_JournalEntryHeader" runat="server">
        <table align="left">
            <tr>
                <td colspan="3" class="blue" align="center">Create a New Journal Entry</td>
            </tr>
            <tr>
                <td>
                    Posting Date
                </td>
                <td>
                    Journal Entry Description
                </td>
                <td>
                    Create:
                </td>
            </tr>
            <tr>
                <td><input type="text" name="Posting_Date" size="12"></td>
                <td><input type="text" name="Description" size="60"></td>
                <td><input type="button" value="Create..." class="button">
                        <input type="hidden" name="Trans_Number"></td>
            </tr>
        </table>
        </form>

    <br/>
    <p class="small">
        New Line By:<br/>
        <a href="#" onclick="showDiv(1);" class="small">WORK ORDER</a><br/>
        <a href="#" onclick="showDiv(0);" class="small">ACCOUNT</a>
    </p>

    <div id="WO" style="visibility:hidden;position:absolute;left:5p x;top:120px;">

    <p>&nbsp;</p>
    <form id="general_journal_entry_WO" method="post">

        <table cellpadding="2" cellspacing="1" border="0">
            <tr>
                <td colspan="4" class="blue">
                    New Line Item: Work Order
                </td>
            </tr>
            <tr>
                <td class="small">WO #:</td>
                <td class="small">WO Description:</td>
                <td class="small">WO Hours:</td>
                <td class="small">WO Amount:</td>
            </tr>
            <tr>
                <td><input type="text" size="9" id="Work_Order_Number" /></td>
                <td><input type="text" size="25" id="Work_Order_Description" /></td>
                <td><input type="text" size="6" id="Work_Order_Hours" /></td>
                <td><input type="text" size="17" id="Work_Order_Amount" />
                            <input type="hidden" name="byWO" value="1"></td>
            </tr>
            <tr>
                <td colspan="4"><input type="submit" value="Submit" class="button"></td>
            </tr>
        </table>

    </form>
    </div>

    <div id="ACCOUNT" style="visibility:hidden;position:absolute;left:5p x;top:120px;">

    <p>&nbsp;</p>
    <form id="general_journal_entry_ACCOUNT" method="post">

        <table cellpadding="2" cellspacing="1" border="0">
            <tr>
                <td colspan="5" class="blue">
                    New Line Item: Account Number
                </td>
            </tr>
            <tr>
                <td class="small">Account #: <a href="javascript:popAccountWindow();">browse...</a></td>
                <td class="small">Description:</td>
                <td class="small">Posting Ref</td>
                <td class="small">Trans Amount:</td>
                <td class="small">Add...</td>
            </tr>
            <tr>
                <td><input type="text" size="9" name="Account_Number"></td>
                <td><input type="text" size="25" id="Account_Description"></td>
                <td><input type="text" size="12" id="Posting_Reference" /></td>
                <td><input type="text" size="17" id="Trans_Amount"></td>
                <td><input type="submit" value="Add Line" class="button"><input type="hidden" name="byAccount" value="1"></td>
            </tr>
        </table>


    </form>
    </div>

    <asp:DataGrid ID="dg_Lines" AutoGenerateColumns="False" Runat="server">
        <Columns>
            <asp:BoundColumn DataField="Line_Number"></asp:BoundColumn>
            <asp:BoundColumn DataField="Description"></asp:BoundColumn>
        </Columns>
    </asp:DataGrid>

  </body>
</html>
[/aspx page]

"A spirit with a vision is a dream with a mission"
__________________
\"A spirit with a vision is a dream with a mission\"
 
Old March 18th, 2004, 01:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

It looks like you are recreating the data table every time... Try storing the data table in the Session or cache, and if it exists in these objects, retrieve it. You could also retrieve the data table from the data source property of the data grid.
 
Old March 18th, 2004, 01:06 PM
Authorized User
 
Join Date: Oct 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thought more about this after I posted and did exactly what you recommended. You're right on; I put the datatable in the minions of sata, I mean, a session variable and it works great now. Thanks for your input.

"A spirit with a vision is a dream with a mission"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Backcolor for separate rows DataRow in Datatable i mikeka VS.NET 2002/2003 0 July 3rd, 2006 08:03 PM
Adding up Column Values within a Datatable rit01 ASP.NET 2.0 Basics 1 May 31st, 2006 11:27 AM
Adding rows, AND column(s) to existing datatable cliffd64 VB.NET 2002/2003 Basics 1 August 18th, 2005 06:50 AM
Use Rows from a DataTable to Create Columns In oth indigolion Classic ASP Professional 0 August 5th, 2005 12:44 PM
Getting the Top 5 Rows from a datatable flyin General .NET 9 June 14th, 2004 08:36 AM





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