Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to get an average of some row totals. I know how to do that, but my average depends on what value is selected in my listbox(lbxwkid)

Conversion from string "" to type 'Double' is not valid. for this line

ElseIf lbxWkid.SelectedValue().ToString > 0 Then

 

HEre is my code the tithecountsum is also a double.

ElseIf lbxWkid.SelectedValue().ToString > 0 Then
ElseIf lbxWkid.SelectedValue().ToString > 0 Then         
Dim lbxValue As Double = Convert.ToDouble(lbxWkid.SelectedValue)
           TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)

Posted

tried that...

 

Tried that I got object reference not set to an instance of an object

 ElseIf lbxWkid.SelectedItem.Selected = True Then
                  Dim lbxValue As Double = Convert.ToDouble(lbxWkid.SelectedItem.Value)
           TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)
       ElseIf lbxWkid.SelectedItem.Selected = False Then
       End If

Posted

re:

 

Which line did the error occur on though? Also is an item selected?

 

It throws the error for this line

ElseIf lbxWkid.SelectedItem.Selected = True Then

 

Here is the full error

 

System.NullReferenceException was unhandled by user code

Message="Object reference not set to an instance of an object."

Source="App_Web_xi7tdrfw"

StackTrace:

at YearlyOff.ComputeSum(Object sender, GridViewRowEventArgs e) in H:\ASPNET20\WebSites\GBCTithe\YearlyOff.aspx.vb:line 57

 

at System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e)

 

at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)

 

at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)

 

at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)

 

at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)

 

at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)

 

at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)

 

at System.Web.UI.WebControls.DataBoundControl.PerformSelect()

 

at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()

 

at System.Web.UI.WebControls.GridView.DataBind()

 

at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()

 

at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()

 

at System.Web.UI.Control.EnsureChildControls()

 

at System.Web.UI.Control.PreRenderRecursiveInternal()

 

at System.Web.UI.Control.PreRenderRecursiveInternal()

 

at System.Web.UI.Control.PreRenderRecursiveInternal()

 

at System.Web.UI.Control.PreRenderRecursiveInternal()

 

at System.Web.UI.Control.PreRenderRecursiveInternal()

 

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Posted

Change:

 

ElseIf lbxWkid.SelectedItem.Selected = True Then

 

To:

 

ElseIf Not lbxWkid.SelectedItem Is Nothing Then

 

Or more commonly seen you could:

 

If Not lblWkik.SelectedIndex = -1 Then

'Code for what to do when there is a selected item (-1 means no selection, so

'thus if it is not -1 then an item has been selected

End If

 

Both will work, it's just a coding style/preference thing. Also you should always have your most common condition in the 'If' clause and non-common conditions in the else clause...so since your selection box will probably have a selection more often than it does not, the part that goes in the 'If' clause would be the checking for whether there is a selected item or not, and the else clause would be the logic for what to do when there isn't something selected. There's no rules on that, just something that most people will say is good practice/coding.

 

What you originally had is telling .NET to test for whether the selected item in the list is selected (which a selected item would be), you want to see if there is a selected item.

Posted

no go...

 

Ok here is everthing I have tried and the results...

ElseIf Not lbxWkid.SelectedItem Is Nothing Then 

Page loads, when I click an item the page postback but nothing shows up in the lblavg text box No errors are thrown.

 

If Not lblWkik.SelectedIndex = -1 Then

Page loads, when I click an item the page postback but nothing shows up in the lblavg text box No errors are thrown.

 

 

ElseIf lbxWkid.SelectedValue <> "" Then 

Page loads, when I click an item the page postback but nothing shows up in the lblavg text box No errors are thrown.

 

If I do this

 

   ElseIf lbxWkid.SelectedIndex > -1 Then
   ElseIf IsNumeric(lbxWkid.SelectedValue) Then

 

Page loads, when I click an item the page postbacks but nothing shows up in the lblavg text box

No errors are thrown

 

When I try this

 

ElseIf lbxWkid.SelectedItem.Text > 0 Then 

Object reference not set to an instance of an object.

 

If I try this

 

ElseIf cdbl(lbxWkid.SelectedValue) > 0 Then 

I get

Input string was not in a correct format.

 

 

Here is my "then" code which I know works because if I take out the if statements and change titheavg = tithecountsum / 20 my lblavg shows the value of titheavg.

 

Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Value)
           TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)

 

This is for asp.net 2.0 I don't think that matters though.

Posted

Ok I figured out some more If I take the if statement and make it a seperate sub and execute that sub on listbox selected change like this (I am using asp.net 2.0)

 

Partial Class YearlyOff
   Inherits System.Web.UI.Page
   Dim TitheCountSum As Double = 0
   Dim EducationCountSum As Double = 0
   Dim MissionCountSum As Double = 0
   Dim SpecialCountSum As Double = 0
   Dim GiftValueCountSum As Double = 0
   Dim OfferingTotal As Double = 0
   Dim DepositTotal As Double = 0
   Dim Weekid As Double = 0
   Dim Datewk As Date
   Dim TitheAvg As Double = 0

Sub ComputeAvg(ByVal Sender As Object, ByVal e As EventArgs)
       If Not lbxWkid.SelectedIndex = -1 Then
           Then
           Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Text)
                      TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = lbxWkid.SelectedValue & String.Format("{0:c}", TitheAvg)
       End If
   End Sub 

My lblavg.text gets $0.00 this is because tithecount sum is a default value of zero before it evals the sumoftithe field. My eval tithcountsum evals that field in another sub.

I even tried to create a new if statement within the same sub and nothing happens like my previous post.

 

Here is all of my code for the sub i originally reffered too This sub is run on rowdatabound

Public Sub ComputeSum(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
      

       If e.Row.RowType = DataControlRowType.DataRow Then
           'ListItemType.item = ListItemType.AlternatingItem Then
           'Snip out the Total of Each Row & Column
           Datewk = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "Date"))
           Weekid = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "WEEK"))
           TitheCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SumofTithe"))
           EducationCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SumofEducation"))
           MissionCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SumofMissions"))
           SpecialCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SumOfSpecial"))
           'GiftValueCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Gift_Value"))
           OfferingTotal = TitheCountSum + MissionCountSum + SpecialCountSum
           DepositTotal = TitheCountSum + MissionCountSum + SpecialCountSum + EducationCountSum
           'lbxValue += Convert.ToDouble(lbxWkid.SelectedValue)
           'Label2.Text = "Tot Offering* " & String.Format("{0:C}", OfferingTotal)
           LblOfferingTot.Text = "Total Offering* " & String.Format("{0:C}", OfferingTotal)
           LblTithe.Text = "Total Tithe: " & String.Format("{0:C}", TitheCountSum)
           LblEducation.Text = "Total Education: " & String.Format("{0:C}", EducationCountSum)
           LblMissions.Text = "Total Missions: " & String.Format("{0:C}", MissionCountSum)
           LblSpecial.Text = "Total Special: " & String.Format("{0:C}", SpecialCountSum)
           LblDepositTot.Text = "Total Deposit* " & String.Format("{0:C}", DepositTotal)
           Lblstar.Text = "Total Offering =Tithe Total + Mission Total + Special Total"
           LblDeposit.Text = "Total Deposit=Tithe Total + Mission Total + Special Total + Education Total"
           

       ElseIf e.Row.RowType = DataControlCellType.Footer Then
           e.Row.RowType = DataControlCellType.Footer
           e.Row.Cells.Item(3).Text = "Total Ti: " & String.Format("{0:C}", TitheCountSum)
           e.Row.Cells.Item(4).Text = "Total Ed: " & String.Format("{0:C}", EducationCountSum)
           e.Row.Cells.Item(5).Text = "Total Mis: " & String.Format("{0:C}", MissionCountSum)
           e.Row.Cells.Item(6).Text = "Total SP: " & String.Format("{0:C}", SpecialCountSum)

[color=DarkRed]'I had the statment I am having trouble with inside this if statment as an ElseIF Like this[/color]
ElseIf Not lbxWkid.SelectedIndex = -1 Then
            Then
           Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Text)
          TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)
           
       End If
[color=DarkRed]'and I Also tried as a seperated If statement in the same sub. Neither of these gives me a value in my lblavg.text[/color] 
       If Not lbxWkid.SelectedIndex = -1 Then
           'ElseIf Not lbxWkid.SelectedItem Is Nothing Then
           Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Text)
           TitheAvg = TitheCountSum / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)
       End If
   End Sub

Posted

Finally

 

FINALLY I got it for whatever reason that if statement would not execute in the computesum sub so I created a hidden field in the compute sum sub, I drew the tithecountsumvalue from another label into the hidden field, rather than use the tithecountsum. I then created a new sub and ran that on the gridview -onprerender Worked like a charm! If there is an easier way that someone knows how to do this feel free to comment but this worked after the tons of things i tried!

Sub ComputeAvg(ByVal Sender As Object, ByVal e As EventArgs)
       If Not lbxWkid.SelectedIndex = -1 Then
             Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Text)
           Dim TitheVal As String = hidTithesum.Value
           TitheAvg = TitheVal / lbxValue
           lblAvg.Text = String.Format("{0:c}", TitheAvg)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...