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> </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> </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"