kana250688 says:
Dim checkitem = sservice.GetByQrSplit(Qty_Stocks)
'error Value of type 'Stocks' cannot convert to string below line to check stock
Dim checkstock As Integer = sservice.GetByQrSplitbysum(Qty_Stocks)
The method GetByQrSplit and GetByQrSplitbysum accepts string as parameter and you are passing the Stocks class object as parameter.
Thats why the error.
You need to pass string value as parameter or change the methods parameter type to class object.
Refer below updated code.
VB.Net
Imports System.Data.OleDb
Imports Dapper
Public Class Form1
Public _myTable As New DataTable()
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
_myTable.Columns.AddRange({
New DataColumn("CodeProduct", GetType(String)),
New DataColumn("CatProd", GetType(String)),
New DataColumn("Barcode", GetType(String)),
New DataColumn("ColorCode", GetType(String))})
End Sub
Private Function parseBarcodeScan(input As String) As List(Of String)
Return input.Split({","}, StringSplitOptions.RemoveEmptyEntries).Select(Function(s) s.Trim()).ToList()
End Function
Private Sub BtxtBarcode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles BtxtBarcode.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
AddEntry()
End If
End Sub
Private Sub AddEntry()
Dim items = parseBarcodeScan(BtxtBarcode.Text)
Try
Dim sservice = New Stockservice()
Dim Qty_Stocks = New Stocks With {
.CodeProduct = items(0),
.CatProd = items(1),
.Barcode = items(2),
.Colorcode = items(3)}
Dim checkitem = sservice.GetByQrSplit(Qty_Stocks)
Dim checkstock As Integer = sservice.GetByQrSplitbysum(Qty_Stocks)
If checkitem Is Nothing Then
MessageBox.Show("Unknown item!")
BtxtBarcode.Clear()
Return
End If
If checkstock < 0 Then
MessageBox.Show("item no stock!")
BtxtBarcode.Clear()
Return
End If
' Where does _BBarcode come from?
_myTable.Rows.Add(items(0), items(1), items(2), items(3))
DataGridView1.DataSource = _myTable
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Public Class Stocks
Public Property CodeProduct() As String
Public Property CatProd() As String
Public Property Barcode() As String
Public Property Colorcode() As String
Public Property Qty_Stock() As Integer
End Class
Public Class Stockservice
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Stocks.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 GetByQrSplit(ByVal code As Stocks) As Stocks
Dim sql = "SELECT * FROM Stocks WHERE CodeProduct = @CodeProduct and CatProd = @CatProd and Barcode = @Barcode and Colorcode = @Colorcode"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Dim dynamicParameters As DynamicParameters = New DynamicParameters()
' Adding Input parameters.
dynamicParameters.Add("@CodeProduct", code.CodeProduct)
dynamicParameters.Add("@CatProd", code.CatProd)
dynamicParameters.Add("@Barcode", code.Barcode)
dynamicParameters.Add("@Colorcode", code.Colorcode)
Return _conn.Query(Of Stocks)(sql, dynamicParameters).FirstOrDefault()
End Using
End Function
Public Function GetByQrSplitbysum(ByVal code As Stocks) As Integer
Dim qtyStock = "SELECT sum(Qty_Stock) AS [Qty_Stock] FROM Stocks WHERE CodeProduct = @CodeProduct and CatProd = @CatProd and Barcode = @Barcode and Colorcode = @Colorcode"
Dim checkitem As Integer = 0
Using _conn = New OleDbConnection(GetOledbConnectionString())
Dim dynamicParameters As DynamicParameters = New DynamicParameters()
' Adding Input parameters.
dynamicParameters.Add("@CodeProduct", code.CodeProduct)
dynamicParameters.Add("@CatProd", code.CatProd)
dynamicParameters.Add("@Barcode", code.Barcode)
dynamicParameters.Add("@Colorcode", code.Colorcode)
Dim o As Object = _conn.ExecuteScalar(qtyStock, dynamicParameters)
If o IsNot Nothing Then
qtyStock = Convert.ToInt32(o)
End If
End Using
Return checkitem
End Function
End Class
Screenshot