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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert listboxvalue to double???? VB.NEt

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
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) I have tried this 2 ways I either get
Conversion from string "" to type 'Double' is not valid. for this line
Code:
ElseIf lbxWkid.SelectedValue().ToString > 0 Then
or if i use
Code:
ElseIf lbxWkid.SelectedItem.Selected = True
I get object not set to a reference
HEre is my code the tithecountsum is also a double.
Code:
ElseIf lbxWkid.SelectedValue().ToString > 0 Then         
 Dim lbxValue As Double = Convert.ToDouble(lbxWkid.SelectedValue)
            TitheAvg = TitheCountSum / lbxValue
            lblAvg.Text = String.Format("{0:c}", TitheAvg)
the other way I tried was
Code:
 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
 
ElseIf lbxWkid.SelectedValue > 0 Then ...



But what do you want to do with the listbox exactly?
 
gt,

You will have an easier time if you pay particular attention to what type of value each of these methods returns.

lbxWkid.SelectedItem will return a value of type ListItem. You might try using SelectedItem.Text

If this doesn't work then you have to assume that your control is not properly defined, populated and databound. Since you have not included this initialization code I can't tell whether the control is properly defined.

Good Luck

 
Need to check to see if any item is selected first because it will cause an error trying to get a value from a non selected dropdown or listbox.

Code:
If dropdown1.SelectedIndex > -1 Then
            If IsNumeric(dropdown1.SelectedValue) Then
                Dim MyVal As Double = CDbl(dropdown1.SelectedValue)
            End If
        End If

You can't compare a string with a numeric.
ElseIf lbxWkid.SelectedValue > 0 Then
Try
ElseIf cdbl(lbxWkid.SelectedValue) > 0 Then


This will always be true if an item is selected:
IF lbxWkid.SelectedItem.Selected = True Then ....
It will also return an error if no item is selected( SelectedItem object not set to a reference)




 
Ok here is everthing I have tried and the results...
Code:
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
Code:
    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
Code:
ElseIf lbxWkid.SelectedItem.Text > 0 Then
Object reference not set to an instance of an object.
Just like Rtomes said
If I try this
Code:
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.
Code:
Dim lbxValue As Double = CDbl(lbxWkid.SelectedItem.Value)
            TitheAvg = TitheCountSum / lbxValue
            lblAvg.Text = String.Format("{0:c}", TitheAvg)

My control is properly bound because if i don't run this code my data shows up in my list box.
This is asp.net 2.0 since you asked here it is
Code:
   <asp:ListBox ID="lbxWkid" runat="server" AutoPostBack="True" DataSourceID="sdsWkid"
                    DataTextField="WKID" DataValueField="WKID" Rows="40" BackColor="DarkGray" ForeColor="DarkGreen"></asp:ListBox></td>
            <td style="width: 100px">
Code:
<asp:SqlDataSource ID="sdsWkid" runat="server" ConnectionString="<%$ ConnectionStrings:ConnGbcTithe %>"
        ProviderName="<%$ ConnectionStrings:ConnGbcTithe.ProviderName %>" SelectCommand="SELECT [WKID] FROM [Weeks]">
    </asp:SqlDataSource>
 
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)
Code:
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
Code:
 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"))
      [COLOR=red]      TitheCountSum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "SumofTithe"))[/color]
            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=red]'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=red'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
 
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!
Code:
 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)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top