Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add integer variable to get total

Posted on 2010-09-17
5
Medium Priority
?
469 Views
Last Modified: 2012-05-10
Hello,
I'm trying to add two numbers together to get a total.
strInst1Fee is a string taken from sqldatareader from a MS SQL money column, but it reads as a string. I want to add $9.00 to that amount, but the $9.00 comes from another database query, so it could change to a different amount.

When I do this, it works:

Dim PriceTot As Integer
        If some variable = 1 Then
            PriceTot = strInst1Fee + 9
End If

PriceTot = 49

This is what I want to do but it does not work:
Dim PriceTot As Integer
        If some variable = 1 Then
            PriceTot = strInst1Fee + strMonthlyFee
End If

PriceTot = 49.00009.0000
but it cannot convert it to an integer, so I get error.

The code below is what I want, but it gives error.

Why does it not add the 2 numbers together like the 1st example? And how do I get it to add the 2 numbers to get 49, when using strInst1Fee + strMonthlyFee?


'get fees from teacher
        Dim strConn As String = "Data Source=mydatasource;Integrated Security=false;Initial Catalog=mycatalog;user ID=mysuer;Password=mypassword"
        Dim sql As String = "Select * From Teacher where TeacherID = @TeacherID"
        Dim conn As New SqlConnection(strConn)
        Dim objDR As SqlDataReader
        Dim Cmd As New SqlCommand(sql, conn)
        Cmd.Parameters.Add(New SqlParameter("@TeacherID", Instrument1.SelectedValue.ToString()))
        conn.Open()
        objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        While objDR.Read()
            strTeacherID = objDR("TeacherID").ToString()
            strInst1Fee = objDR("Inst1Fee").ToString()
            strInst2Fee = objDR("Inst2Fee").ToString()
            strInst3Fee = objDR("Inst3Fee").ToString()
            strInst4Fee = objDR("Inst4Fee").ToString()
            strInst5Fee = objDR("Inst5Fee").ToString()
            strInst6Fee = objDR("Inst6Fee").ToString()
            strInst7Fee = objDR("Inst7Fee").ToString()
           
        End While

        conn.Close()

        'get monthly admin fee to add to teacher fee
        Dim strConn2 As String = "Data Source=mydatasource.net;Integrated Security=false;Initial Catalog=mycatalog;user ID=usr;Password=password"
        Dim sql2 As String = "Select * From Admin where TeacherID = @TeacherID"
        Dim conn2 As New SqlConnection(strConn2)
        Dim objDR2 As SqlDataReader
        Dim Cmd2 As New SqlCommand(sql2, conn2)
        Cmd2.Parameters.Add(New SqlParameter("@TeacherID", Instrument1.SelectedValue.ToString()))
        conn2.Open()
        objDR2 = Cmd2.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        While objDR2.Read()
            strMonthlyFee = objDR2("MonthlyFee").ToString()

        End While

        conn2.Close()

        'get fee for instrument from total of teacher fee plus admin fee
        'for monthly recurring billing
        Dim PriceTot As Integer
        If Teacher1.SelectedValue.ToString() = 1 Then
            PriceTot = strInst1Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 2 Then
            PriceTot = strInst2Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 3 Then
            PriceTot = strInst3Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 4 Then
            PriceTot = strInst4Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 5 Then
            PriceTot = strInst5Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 6 Then
            PriceTot = strInst6Fee + strMonthlyFee
        ElseIf Teacher1.SelectedValue.ToString() = 7 Then
            PriceTot = strInst7Fee + strMonthlyFee
        End If

Open in new window

0
Comment
Question by:mlg101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 1000 total points
ID: 33704656
use convert
declare @s1 varchar(128), @s2 varchar(128)

select @s1='$49.0000', @s2='9.0000'

select convert(money, @s1)+convert(money, @s2)
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 1000 total points
ID: 33704680
Try this:

PriceTot = Val(strInst1Fee) + Val(strMonthlyFee)

If strMonthlyFee comes with $ sign as the first character, then try this:

PriceTot = Val(strInst1Fee) + Val(strMonthlyFee.SubString(1))

0
 
LVL 1

Author Comment

by:mlg101
ID: 33704714
I clicked on accept multiple solutions, but it made me close it???? something buggy is going on
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question