Dear Sir,
why Input string was not in a correct format in my textbox with VB.NET?
Is there something wrong with my code?
Please Guide me.
Thanks
Code in Form1
Imports System.ComponentModel
Imports System.Data.OleDb
Imports Dapper
Public Class Form1
Private bindingSource As BindingSource = Nothing
Private salesservices As New SalesService()
Private Sub CalculateTotalValue()
Dim tot As Double = 0
For Each item As DataGridViewRow In DataGridView1.Rows
Dim val As Double
Dim val2 As Double
Dim val3 As Double
Double.TryParse(CType(item.Cells("Qty").Value, String), val)
Double.TryParse(CType(item.Cells("Price").Value, String), val2)
Double.TryParse(CType(item.Cells("Discount").Value, String), val3)
tot += (val * val2) - (val3)
Next item
lblValue.Text = tot.ToString("N2")
'lblChange.Text = (cash - tot).ToString("N2")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Txtinvono.Text = "1000"
DataGridView1.AllowUserToAddRows = False
bindingSource = New BindingSource With {.DataSource = New BindingList(Of Salesdetail)(CType(salesservices.GetSalesdetail(Txtinvono.Text), IList(Of Salesdetail)))}
DataGridView1.DataSource = bindingSource
DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
CalculateTotalValue()
End Sub
Private Sub BtnPayment_Click(sender As Object, e As EventArgs) Handles BtnPayment.Click
Using frm = New Payment()
frm.LblInvono.Text = Me.Txtinvono.Text
If frm.ShowDialog() = DialogResult.OK Then
End If
End Using
End Sub
End Class
Public Class Sales
Public Property Invno() As String
Public Property DateInvno() As Date
Public Property Totalinv() As Integer
Public Property DiscountTotal() As Integer
Public Property Paymentofterm() As String
Public Property CashPayment() As Integer
Public Property NCashPayment() As Integer
Public Property Sales() As New List(Of Salesdetail)()
End Class
Public Class Salesdetail
Public Property No() As Integer
Public Property Invno() As String
Public Property CodeProduct() As String
Public Property Qty() As Integer
Public Property Price() As Integer
Public Property Discount() As Integer
Public Property Total() As Integer
End Class
Public Class SalesService
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Sales.accdb;Persist Security Info=False;"
End Function
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetSalesdetail(ByVal Invno As String) As IEnumerable(Of Salesdetail)
Dim sql = $"SELECT * FROM Salesdetail WHERE Invno = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Salesdetail)(sql).ToList()
End Using
End Function
Public Function GetSales(ByVal Invno As String) As Sales
Dim sql = $"SELECT * FROM Sales WHERE Invno = '{Invno}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Sales)(sql).FirstOrDefault()
End Using
End Function
End Class
Code in Payment
Public Class Payment
Private salesservices As New SalesService()
Private Sub Payment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sales = salesservices.GetSales(LblInvono.Text)
LabelTotalvalue.Text = sales.Totalinv.ToString("N0")
txtDiscount.Text = sales.DiscountTotal.ToString("N0")
txtPayment.Text = (sales.CashPayment + sales.NCashPayment).ToString("N0")
LabelChange.Text = ((sales.CashPayment + sales.NCashPayment) - sales.Totalinv).ToString("N0")
End Sub
Private Sub OnTextChanged(sender As Object, e As EventArgs) Handles txtPayment.TextChanged, txtDiscount.TextChanged
Dim payment As Double = 0
Dim discount As Double = 0
If Not String.IsNullOrEmpty(txtPayment.Text) OrElse Not String.IsNullOrEmpty(txtDiscount.Text) Then
payment = Convert.ToDouble(txtPayment.Text)
discount = Convert.ToDouble(txtDiscount.Text)
LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - payment).ToString("N0")
Else
LabelChange.Text = "-" & Convert.ToDouble(LabelTotalvalue.Text).ToString("N0")
End If
End Sub
Private Sub OnText_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPayment.KeyPress, txtDiscount.KeyPress
If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
End If
End Sub
Private Sub txtPayment_KeyUp(sender As Object, e As KeyEventArgs) Handles txtPayment.KeyUp
txtPayment.Text = txtPayment.Text.Replace(",", "")
Dim discount As Double = 0
Dim payment As Double = 0
If Not String.IsNullOrEmpty(txtPayment.Text) Then
'error in line code below
payment = Convert.ToDouble(txtPayment.Text)
LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - payment).ToString("N0")
txtPayment.Text = Convert.ToDouble(txtPayment.Text).ToString("N0")
txtPayment.[Select](txtPayment.Text.Length, 0)
End If
End Sub
Private Sub txtDiscount_KeyUp(sender As Object, e As KeyEventArgs) Handles txtDiscount.KeyUp
txtDiscount.Text = txtPayment.Text.Replace(",", "")
Dim discount As Double = 0
Dim payment As Double = 0
If Not String.IsNullOrEmpty(txtPayment.Text) Then
discount = Convert.ToDouble(txtDiscount.Text)
LabelChange.Text = (Convert.ToDouble(LabelTotalvalue.Text) - discount - payment).ToString("N0")
txtDiscount.Text = Convert.ToDouble(txtDiscount.Text).ToString("N0")
txtDiscount.[Select](txtPayment.Text.Length, 0)
End If
End Sub
End Class