I have a project where I want to be able to multiply 2 values from a database.

So I have a combo box where the user is able to select from a set of data.

This displays the data relating to the selected row in text boxes on the windows form.

There is a button that is clicked that needs to multiply the one column by another so essentially the text from box 1 against the text from box 2.

I have tried a few ways of doing this now but keep getting errors stating the number must be less than infinity or that the source must be convertible to the destination type.

I know this is probably a school boy error but I am admitting defeat...

Lets see the code that is throwing the error.

Private Sub btnTotalInvestment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotalInvestment.Click

' strSql is a SQL statement that selects all the fields from the Stocks table
Dim strSql As String = "SELECT * FROM Stocks "
' strPath provides the datatbase type and the path of the Kids Stocks database
Dim strPath As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;" _
& "Data Source =I:\Uni\Module 3 - Software Development\Assignment 5 - Kid Friendly Stocks\kidfriendlystocks.accdb"
Dim odaStocks As New OleDb.OleDbDataAdapter(strSql, strPath)

Dim datInvestment As New DataTable
' Dim intCount As Integer
Dim decTotalInvestment As Decimal = 0D
Dim decShares As Decimal = CDec(("Number_of_sharesTextBox"))
Dim decPrice As Decimal = CDec(("Price_per_shareTextBox"))


' The DataTable name datInvestment is filled with the table data
odaStocks.Fill(datInvestment)
' The connection to the database is disconnected
odaStocks.Dispose()

'For intCount = 0 To datInvestment.Rows.Count - 1
'decTotalInvestment += Convert.ToDecimal(datInvestment.Rows(intCount)("Price Per Share"))
decTotalInvestment += (decShares * decPrice)
'Next
Me.lblTotalInvestment.Visible = True
Me.lblTotalInvestment.Text = "The Total Investment is " _
& decTotalInvestment.ToString("C")

End Sub

Below is the area in the Event I just posted where the error is highlighted:

Dim decShares As Decimal = CDec(("Number_of_sharesTextBox"))
Dim decPrice As Decimal = CDec(("Price_per_shareTextBox"))

CDec(("Number_of_sharesTextBox")) <<< you are trying to convert the string "Number_of_sharesTextBox" to decimal. This wont work out.

Maybe you tell us what Number_of_sharesTextBox is. a Textbox? if so then it sould be:
CDec(Number_of_sharesTextBox.Text)

Thanks,

I read over some old work and realised I was overcomplicating things because I am using a Database with VB for the first time.

Here is what I have now that works a treat - thanks again for your help:

Dim strNumberOfShares As String
Dim strPricePerShare As String
Dim decNumberOfShares As Decimal
Dim decPricePerShare As Decimal

strNumberOfShares = Me.txtNumberOfShares.Text
strPricePerShare = Me.txtPricePerShare.Text
decNumberOfShares = Convert.ToDecimal(strNumberOfShares)
decPricePerShare = Convert.ToDecimal(strPricePerShare)

Dim decTotalInvestment = decNumberOfShares * decPricePerShare

Me.lblTotalInvestment.Visible = True
Me.lblTotalInvestment.Text = "The Total Investment is " _
& decTotalInvestment.ToString("C")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.