textbox implicit conversion from decimal to string - vb.net

I have this error
implicit conversion from decimal to string during compilation
Public Sub InvoicxeHT()
Dim Total1 As Decimal = "0,00"
For Each row As DataGridViewRow In DataGridView1.Rows
Total1 += row.Cells(4).Value
Next
TotalHT.Text = Total1
End Sub
can i know whats the problem

Try this:
Public Sub InvoicxeHT()
Dim Total1 As Decimal = "0,00"
For Each row As DataGridViewRow In DataGridView1.Rows
Total1 += CDec(row.Cells(4).Value) ' Or CInt()
Next
TotalHT.Text = Total1
End Sub

Related

Input string was not in a correct format in vb,net

I have an error problem in the text label. Is there another solution?
Thanks
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double = Double.Parse(lblDisTotal.Text)
For Each item As DataGridViewRow In grid.Rows
tot += Double.Parse(item.Cells(5).Value.ToString())
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = ""
End Sub
when the button event is refreshed then lblDisTotal does not become blank but becomes "0" then the problem is solved
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double = Double.Parse(lblDisTotal.Text)
For Each item As DataGridViewRow In grid.Rows
tot += Double.Parse(item.Cells(5).Value.ToString())
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = "0"
End Sub
The appropriate solution would be Double.TryParse to handle someone typing "hello world" into your text box if it is not sanitized.
As a rule, you should avoid .parse whereever possible and opt for try parse unless you can guarantee with absolute certainty that your value CAN be parsed into the type you want. Otherwise, utilize tryParse to prevent exceptions that can be prevented.
Private Sub CalculateGrandTotal()
Dim tot As Double = 0
'error this below code
Dim cash As Double
'tryParse returns a boolean indicating successful parsing. You can check for that, however, if it fails, it assigns 0 to your passed in double
Double.TryParse(lblDisTotal.Text, cash)
For Each item As DataGridViewRow In grid.Rows
dim val as double
Double.TryParse(item.Cells(5).Value, val)
tot += val
Next item
lblGrandTotal.Text = (tot * (1 - cash / 100)).ToString("N2")
End Sub
Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
lblDisTotal.Text = ""
End Sub

How To Sum DataGridView Amount Column auto VB.NET

How i Sum Datagridview Column auto sum when i adding the digit i have use the below code but not working
Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
Dim sum = (From row As DataGridViewRow In DataGridView1.Rows.Cast(Of DataGridViewRow)() _
Select CDec(row.Cells(1).Value)).Sum
TextBox1.Text = sum.ToString
End Sub
Dim sum As Integer = 0
For i As Integer = 0 To DataGridView1.Rows.Count() - 1 Step +1
sum = sum + DataGridView1.Rows(i).Cells(2).Value
Next
TextBox5.Text = sum.ToString()
try like this
Dim total As Integer = DataGridView1.Rows.Cast(Of DataGridViewRow)().Sum(Function(t) Convert.ToInt32(t.Cells(1).Value))
TextBox1.Text = total.tostring()

Select Distinct values from datagridview1 and pass to datagridview2 in vb.net

How can i select distinct values from datagridview1 and pass this values to datagridview2?
I had this image i upload, would you mind to take a look for more details to my question;
Scenario
I already tried this code but it seems not working;
Private Sub GetLabandOtherFees()
Dim DistinctValues() As String = (From row As DataGridViewRow In dgvSubjectsEnrolled.Rows.Cast(Of DataGridViewRow)() _
Where Not row.IsNewRow _
Select CStr(row.Cells(0).Value)).Distinct.ToArray
For Each row As DataGridViewRow In dgvsub.Rows
dgvsub.Rows(row.Index).Cells(0).Value = DistinctValues
Next
End Sub
Thanks for the help.
Try this one.
Private Sub GetLabandOtherFees()
Dim dic As New Dictionary(Of String, Integer)()
Dim cellValue As String = Nothing
For i As Integer = 0 To datagridview1.Rows.Count - 1
If Not datagridview1.Rows(i).IsNewRow Then
cellValue = datagridview1(0, i).Value.ToString()
If Not dic.ContainsKey(cellValue) Then
dic.Add(cellValue, 1)
Else
dic(cellValue) += 1
End If
End If
Next
Dim sb As New StringBuilder()
For Each keyvalue As KeyValuePair(Of String, Integer) In dic
sb.AppendLine(String.Format("{0}", keyvalue.Key, keyvalue.Value))
Next
For Each row As DataGridViewRow In datagridview2.Rows
row.Cells(0).Value = sb.ToString()
row.Cells(1).Value = dic.Count.ToString()
Next
End Sub

Decimal to integer conversion error

I'm working on a school project using VIsual Studio 2015, the assignment is to create GUI "Commute Calculator" with three options for mode of transportation. I've wrote the code following the instructions in my textbook but am getting the following error:
"BC30057 Too many arguments to 'Private Function CarFindCost(intCommuteChoice As Integer, intDays As Integer) As Decimal'."
I'm a newbie to vs, but based on the error I believe the problem is with how I declared variables. I googled how to convert an integer to decimal, but haven't found anything that worked. The code is lengthy, but I included it all as an FYI. The error is in the private sub btnCommute and appears to be tied to three private functions: CarFindCost, BusFindCost and TrainFindCost. How to I fix it so I don't get errors on the variable intLength in the private sub bthCommute?
Option Strict On
Public Class frmCommuteCalc
Dim intCommuteChoice As Integer
Dim strSelectedMode As String = ""
Private _strGas As Integer
Private _strMiles As String = "Enter the total miles for a round trip: "
Private _strMilesPerGallon As Double = 2.15
Private _strDailyParking As Decimal = 10
Private _strMonthlyParking As Decimal
Private _strMonthlyUpkeep As Decimal = 112
Private _strRTBusFare As String = "Round trip bus fare is "
Private _strRTTrainFare As String = "Round trip train fare is "
Private _StrDays As String = "Enter the number of days worked per month: "
Private _intTrainFare As Integer
Private Sub frmCommuteCalc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Threading.Thread.Sleep(5000)
End Sub
Private Sub cboCommuteMethod_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboCommuteMethod.SelectedIndexChanged
Dim intCommuteChoice As Integer
intCommuteChoice = cboCommuteMethod.SelectedIndex()
lstCommute.Items.Clear()
Select Case intCommuteChoice
Case 0
Car()
Case 1
Train()
Case 2
Bus()
End Select
lblDays.Visible = True
lblMiles.Visible = True
lblLength.Visible = True
txtDays.Visible = True
'txtMonthlyTotal.Visible = True
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim intCommuteChoice As Integer
Dim intDaysPerMonth As Integer
Dim decTotalCost As Decimal
Dim intLength As Integer = 0
Dim strSelectedMode As String = ""
Dim blnNumberInDaysIsValid As Boolean = False
Dim blnCommuteMethodIsSelected As Boolean = False
blnNumberInDaysIsValid = ValidateNumberInDays()
intCommuteChoice = ValidateCommuteSelection(blnCommuteMethodIsSelected, strSelectedMode)
If (blnNumberInDaysIsValid And blnCommuteMethodIsSelected) Then
intDaysPerMonth = Convert.ToInt32(txtDays.Text)
intCommuteChoice = cboCommuteMethod.SelectedIndex()
Select Case intCommuteChoice
Case 0
decTotalCost = CarFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 1
decTotalCost = BusFindCost(intCommuteChoice, intDaysPerMonth, intLength)
Case 2
decTotalCost = TrainFindCost(intCommuteChoice, intDaysPerMonth, intLength)
End Select
End If
End Sub
Private Function intLength() As Object
Throw New NotImplementedException()
End Function
Function ComputeCommuteCost(ByVal decMiles As Decimal, ByVal decGallons As Decimal) As Decimal
Dim decMilage As Decimal
decMilage = decMiles / decGallons
Return decMilage
End Function
Private Sub Car()
lstCommute.Items.Add(_strMiles)
lstCommute.Items.Add(_strMilesPerGallon)
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strMonthlyParking)
lstCommute.Items.Add(_strMonthlyUpkeep)
End Sub
Private Sub Bus()
lstCommute.Items.Add(_strRTBusFare)
lstCommute.Items.Add(_StrDays)
End Sub
Private Sub Train()
lstCommute.Items.Add(_StrDays)
lstCommute.Items.Add(_strRTTrainFare)
End Sub
Private Function ValidateNumberInDays() As Boolean
Dim intDays As Integer
Dim blnValidityCheck As Boolean = False
Dim strNumberInDaysMessage As String = "Please enter the No. of days per month you will be commuting "
Dim strMessageBoxTitle As String = "Error"
Try
intDays = Convert.ToInt32(txtDays.Text)
If intDays >= 1 And intDays <= 21 Then
blnValidityCheck = True
Else
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End If
Catch Exception As FormatException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As OverflowException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
Catch Exception As SystemException
MsgBox(strNumberInDaysMessage, , strMessageBoxTitle)
txtDays.Focus()
txtDays.Clear()
End Try
Return blnValidityCheck
End Function
Private Function ValidateCommuteSelection(ByRef blnDays As Boolean, ByRef strDays As String) As Integer
Dim intCommuteChoice As Integer
Try
intCommuteChoice = Convert.ToInt32(lstCommute.SelectedIndex)
strDays = lstCommute.SelectedItem.ToString()
blnDays = True
Catch Exception As SystemException
MsgBox("Select a commute mode", , "Error")
blnDays = False
End Try
Return intCommuteChoice
End Function
Private Function CarFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim decDaysPerMonth As Decimal
Dim decMiles As Decimal
Dim decMilesPerGallon As Decimal = 2
Dim decGasTotal As Decimal
Dim decDailyParking As Decimal = 10
Dim decMonthlyParking As Decimal
Dim decMonthlyUpkeep As Decimal = 112
Dim decFinalCost As Decimal
Dim intLength As Integer = 0
decMiles = Convert.ToDecimal(txtMiles.Text)
decMilesPerGallon = Convert.ToDecimal(txtGallons.Text)
decGasTotal = decMilesPerGallon * decMiles
decMonthlyParking = Convert.ToDecimal(lblLength.Text)
decMonthlyParking = decDailyParking * decDaysPerMonth
decFinalCost = Convert.ToDecimal(lblLength.Text)
decFinalCost = decGasTotal + decMonthlyUpkeep + decMonthlyParking
Return decFinalCost
End Function
Private Function BusFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decBusFarePerDay As Decimal = 4
Dim decFinalCost As Decimal
decBusFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decBusFarePerDay * decDaysPerMonth
Return decFinalCost
End Function
Private Function TrainFindCost(ByVal intCommuteChoice As Integer, ByVal intDays As Integer) As Decimal
Dim intLength As Integer = 0
Dim decDaysPerMonth As Decimal
Dim decTrainFarePerDay As Decimal = 18
Dim decFinalCost As Decimal
decTrainFarePerDay = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = Convert.ToDecimal(txtMonthlyTotal)
decFinalCost = decDaysPerMonth * decTrainFarePerDay
Return decFinalCost
End Function
End Class
In:
Select Case intCommuteChoice ... End Select
in each case; call function CarFindCost with 3 parameters y you have declared with only 2.

VBA Error BC30452 Operator 'Mod' is not defined for types 'Char' and 'Integer'

the following code is getting the above error and i cant see why. i know the mod operator needs numbers in front and behind it so chrArray(i) is not getting a number. any help appreciated.
Public Class Form1
Private Sub Button3_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)
Textbox1.Text = ""
Textbox2.Text = ""
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Dim sNumber As String
sNumber = TextBox1.Text
Textbox2.Text = EvenDigitMessage(sNumber)
End Sub
Private Function IsOnlyNumber(ByVal qty As String) As Boolean
Dim objRegExp As New System.Text.RegularExpressions.Regex("^\d+$")
Return objRegExp.Match(qty).Success
End Function
Private Function IsThreeDigitNumber(ByVal num As String) As Boolean
Dim objRegExp As New System.Text.RegularExpressions.Regex("^\d{3}$")
Return objRegExp.Match(num).Success
End Function
Private Function EvenDigitMessage(ByVal sNumber As String) As String
Dim bValidDigits As Boolean
Dim bValidNumber As Boolean
Dim sMessage As String
Dim iTotalEven As Integer = 0
If Not IsNumeric(sNumber) Then
sMessage = "Please enter a 3 digit number"
Else
bValidNumber = IsOnlyNumber(sNumber)
bValidDigits = IsThreeDigitNumber(sNumber)
If bValidNumber = False Or bValidDigits = False Then
sMessage = "Please enter a 3 digit number"
Else
iTotalEven = CountEvenDigits(sNumber)
sMessage = "This number contains" & iTotalEven & "even digits."
End If
End If
Return sMessage
End Function
Private Function CountEvenDigits(ByVal sNumber As String) As Integer
Dim i As Integer
Dim chrArray() As Char
Dim iTotal As Integer = 0
chrArray = sNumber.ToCharArray
For i = 0 To chrArray.Length - 1
If chrArray(i) Mod 2 = 0 Then
If iTotal = 0 Then
iTotal = 1
Else
iTotal = iTotal + 1
End If
End If
Next
Return iTotal
End Function
End Class
Base on this linke
https://msdn.microsoft.com/en-us/library/7sx7t66b.aspx
Visual Basic does not convert directly between Char and the numeric types. You can use the Asc or AscW function to convert a Char value to an Integer that represents its code point. You can use the Chr or ChrW function to convert an Integer value to a Char that has that code point.
If the type checking switch (Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the Char data type. The following example illustrates this.
so you have to change your code to somthing like this
If Asc(chrArray(i)) Mod 2 = 0 Then
If iTotal = 0 Then
iTotal = 1
Else
iTotal = iTotal + 1
End If
End If