Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems obtaining running total in datagrid using VB

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
Please help,

I am having trouble getting a running total of a specific column in the datagrid. I get an error indicating the input string was not in a correct format (see below). I'm not sure what I have to do. I tried stepping through the code with the debugger to isolate the problem, but I'm still not sure what is going on.

-----------------------------
ERROR MESSAGE
-----------------------------

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 78: If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Line 79: CalcTotal(e.Item.Cells(11).Text)
Line 80: e.Item.Cells(11).Text = String.Format("{0:c}", Convert.ToDouble(e.Item.Cells(11).Text))
Line 81: ElseIf (e.Item.ItemType = ListItemType.Footer) Then
Line 82: e.Item.Cells(10).Text = "Total"

-----------------------------
STACK TRACE
-----------------------------
Input string was not in a correct format.
at System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info)
at System.Double.Parse(String s, NumberStyles style, IFormatProvider provider)
at System.Convert.ToDouble(String value)
at NewLeafTech.AccountsReceivable.dgAR_ItemDataBound(Object sender, DataGridItemEventArgs e) in c:\inetpub\ 80
at System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e)
at System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32 dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem, DataGridColumn[] columns, TableRowCollection rows, PagedDataSource pagedDataSource)
at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.BaseDataList.DataBind()
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBind()
at NewLeafTech.AccountsReceivable.GetData() in c:\inetpub\ 63
at NewLeafTech.AccountsReceivable.Page_Load(Object sender, EventArgs e) in c:\inetpub\ 42
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()

-----------------------------
CODE BEHIND FILE
-----------------------------

Imports NewLeafTech

Public Class AccountsReceivable
Inherits System.Web.UI.Page
Protected WithEvents dgAR As System.Web.UI.WebControls.DataGrid
Protected WithEvents btSubmit As System.Web.UI.WebControls.Button
Private runningTotal As Double = 0

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If IsPostBack = False Then
GetData()
End If

End Sub

Sub GetData()

Dim o_AR As New NewLeafTech.AccountsReceivable()
With dgAR
.DataSource = o_AR.GetAccountReceivableAll
End With

Dim o_Stats As New NewLeafTech.EarningStats()
With dgProjStats
.DataSource = o_Stats.GetProjectStats
End With

With dgStatTotals
.DataSource = o_Stats.GetStatTotals
End With

DataBind()

End Sub

Private Sub CalcTotal(ByVal _paycheck As String)

Try
runningTotal += Double.Parse(_paycheck)
Catch
' A value was null
End Try
End Sub

Public Sub dgAR_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
CalcTotal(e.Item.Cells(11).Text)
e.Item.Cells(11).Text = String.Format("{0:c}", Convert.ToDouble(e.Item.Cells(11).Text))
ElseIf (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(10).Text = "Total"
e.Item.Cells(11).Text = String.Format("{0:c}", runningTotal)
End If

End Sub

Other routines removed to save on space.

End Class

-----------------------------
ASPX FILE
-----------------------------

<asp:datagrid
id=&quot;Datagrid1&quot;
runat=&quot;server&quot;
OnEditCommand=&quot;EditAccountsReceivable&quot;
OnCancelCommand=&quot;CancelAccountsReceivable&quot;
OnUpdateCommand=&quot;UpdateAccountsReceivable&quot;
OnDeleteCommand=&quot;DeleteAccountsReceivable&quot;
DataKeyField=&quot;ID&quot;
OnItemDataBound=&quot;dgAR_ItemDataBound&quot;
AutoGenerateColumns=&quot;False&quot;
ShowFooter=&quot;True&quot;
Width=&quot;100%&quot;
BorderColor=&quot;#CCCCCC&quot;
BorderStyle=&quot;None&quot;
BorderWidth=&quot;1px&quot;
BackColor=&quot;White&quot;
CellPadding=&quot;3&quot;>
<SelectedItemStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BackColor=&quot;#669999&quot;></SelectedItemStyle>
<AlternatingItemStyle BackColor=&quot;#EDE9E0&quot;></AlternatingItemStyle>
<ItemStyle ForeColor=&quot;#000066&quot; Font-Name=&quot;verdana&quot; Font-Size=&quot;12px&quot;></ItemStyle>
<HeaderStyle Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; BackColor=&quot;#006699&quot; Font-Name=&quot;verdana&quot; Font-Size=&quot;12px&quot;></HeaderStyle>
<FooterStyle ForeColor=&quot;#000066&quot; BackColor=&quot;White&quot;></FooterStyle>
<Columns>
<asp:BoundColumn Visible=&quot;False&quot; DataField=&quot;ID&quot; ReadOnly=&quot;True&quot;></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Week Of Date
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem(&quot;WeekOfDate&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;70&quot; ID=&quot;Textbox1&quot; Text='<%# Container.DataItem( &quot;WeekOfDate&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Client Name
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;ClientName&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;100&quot; ID=&quot;Textbox2&quot; Runat=&quot;server&quot;></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Project Name
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;ProjectName&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;100&quot; ID=&quot;Textbox3&quot; Runat=&quot;server&quot;></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Company Name
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;CompanyName&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;100&quot; ID=&quot;Textbox4&quot; Runat=&quot;server&quot;></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Billable Hours
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;BillableHours&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;30&quot; ID=&quot;Textbox5&quot; Text='<%# Container.DataItem(&quot;BillableHours&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Extra Hours
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;ExtraHours&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;30&quot; ID=&quot;Textbox6&quot; Text='<%# Container.DataItem(&quot;ExtraHours&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Hourly Rate
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;HourlyRate&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;50&quot; ID=&quot;Textbox7&quot; Text='<%# Container.DataItem(&quot;HourlyRate&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Tax Type
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;TaxType&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList width=&quot;100&quot; id=&quot;Dropdownlist1&quot; runat=&quot;server&quot;>
<asp:ListItem Value=&quot;W-2&quot; Selected=&quot;True&quot;>W-2</asp:ListItem>
<asp:ListItem Value=&quot;1099&quot;>1099</asp:ListItem>
<asp:ListItem Value=&quot;None&quot;>None</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Check Number
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;CheckNumber&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;70&quot; ID=&quot;Textbox8&quot; Text='<%# Container.DataItem(&quot;CheckNumber&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Reference Number
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;ReferenceNumber&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;70&quot; ID=&quot;Textbox9&quot; Text='<%# Container.DataItem(&quot;ReferenceNumber&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Paycheck Amount
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;PaycheckAmount&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;50&quot; ID=&quot;Textbox10&quot; Text='<%# Container.DataItem(&quot;PaycheckAmount&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Payment Date
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;PaymentDate&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;70&quot; ID=&quot;Textbox11&quot; Text='<%# Container.DataItem(&quot;PaymentDate&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
Comments
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItem( &quot;Comments&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;125&quot; ID=&quot;Textbox12&quot; Text='<%# Container.DataItem(&quot;Comments&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType=&quot;LinkButton&quot; UpdateText=&quot;Update&quot; CancelText=&quot;Cancel&quot; EditText=&quot;Edit&quot;>
<ItemStyle Width=&quot;70px&quot;></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot;>
<ItemStyle Width=&quot;50px&quot;></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid> regards,
Brian
 
That error points to the fact that you have a null in there somewhere that you're trying to format to a particular string...

Anywhere you're going to format something, and there's any chance that there will ever be a null there instead of a value, you always need to preface it with:

if isdbnull(field) then
'format your string
else
'return an empty string
end if

You can perform this in declarative databinding expressions, too, by declaring a function that will return the string in the code-behind, and making a call to that function in your databinding expression:

<%# returnFormattedString(Container.DataItem(&quot;field&quot;)) %>

~~~

protected function returnFormattedString(o as object) as string
dim output as string
if not isdbnull(o) then
output = String.Format(&quot;{0:c}&quot;, Convert.ToDouble(o))
else
output = string.empty
end if
return output
end function
penny1.gif
penny1.gif
 
Thanks, I will try that and get back to you. regards,
Brian
 
How can I declare this function call on the current page?

Compiler Error Message: BC30451: Name 'returnFormattedString' is not declared.

Source Error:



Line 156: </HeaderTemplate>
Line 157: <ItemTemplate>
Line 158: <%# returnFormattedString(Container.DataItem( &quot;PaycheckAmount&quot;)) %>
Line 159: </ItemTemplate>
Line 160: <EditItemTemplate>

regards,
Brian
 
I am still getting an error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 69: Dim output As String
Line 70: If Not isdbnull(o) Then
Line 71: output = String.Format(&quot;{0:c}&quot;, Convert.ToDouble(o))
Line 72: Else
Line 73: output = String.empty


Source File: c:\inetpub\ Line: 71


IN ASPX FILE
<asp:TemplateColumn>
<HeaderTemplate>
Paycheck Amount
</HeaderTemplate>
<ItemTemplate>
<%# returnFormattedString(Container.DataItem( &quot;PaycheckAmount&quot;)) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Width=&quot;50&quot; ID=&quot;txtPayAmt&quot; Text='<%# Container.DataItem(&quot;PaycheckAmount&quot;) %>' runat=&quot;server&quot;/>
</EditItemTemplate>
</asp:TemplateColumn>

IN CODE BEHIND FILE


Protected Function returnFormattedString(ByVal o As Object) As String

Dim output As String
If Not isdbnull(o) Then
output = String.Format(&quot;{0:c}&quot;, Convert.ToDouble(o))
Else
output = String.empty
End If
Return output

End Function

Private Sub CalcTotal(ByVal _paycheck As String)

Try
runningTotal += Double.Parse(_paycheck)
Catch
' A value was null
End Try
End Sub

Public Sub dgAR_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
CalcTotal(returnFormattedString(e.Item.Cells(11).Text))
e.Item.Cells(11).Text = String.Format(&quot;{0:c}&quot;, e.Item.Cells(11).Text)
ElseIf (e.Item.ItemType = ListItemType.Footer) Then
e.Item.Cells(10).Text = &quot;Total&quot;
e.Item.Cells(11).Text = String.Format(&quot;{0:c}&quot;, runningTotal)
End If

End Sub

regards,
Brian
 
well then what is the value of o when it tries to format it?

Typically, it will mean a NULL value, but that's not what it has to mean.

My suggestion is to put a break point on that routine, and debug on the value of o.

See what it is, and the problem should be readily apparent.

-paul
penny1.gif
penny1.gif
 
Well, the Christmas holiday is over and I'm back to work on this problem. The values in the table are as follows. There are no NULL values in that field.

300.0000
.0000
2240.0000
80.0000
.0000
3080.0000
40.0000
.0000
.0000
3080.0000
40.0000
250.0000
.0000
3430.0000
.0000
.0000
.0000
400.0000
1500.0000
.0000
2025.0000

When my routine is going through each value it is doing a String.Format, but the values in the database are a Money datatype. If this is the case, why should I have a String.Format and not something like a Double format of some sort? thanks


regards,
Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top