Search code examples
vb.netbindingsource

BindingSource.Count always display as 1 unless it is not null VB.Net


I'm developing a program to keep stocks. I need to Update the stock of table if the ShoeID and Size are already excisting.Otherwise it should add a new row.

When I enter data, if the same shoeID and Size in previous record, it updates. But if I add the record after some records it creates a new record. Does not update the previous record.

Private Sub AddStockBtn_Click(sender As Object, e As EventArgs) Handles AddStockBtn.Click
    Dim Size, Stock As Integer
    Dim stock_check As String
    Dim status As Boolean = False
    stock_check = "Pending"
    StockBindingSource.ResetBindings(True)

    Try
        Size = Integer.Parse(SizeTxt.Text)
        Stock = Integer.Parse(StockTxt.Text)
        Console.WriteLine(stock_check)
    Catch ex As Exception
        MessageBox.Show("Invalid Size or Stock")
    End Try

    If (StockBindingSource.Count = 0) Then
        StockBindingSource.AddNew()
        StockBindingSource.Current("No") = 1
        StockBindingSource.Current("ShoeID") = IDBox.Text
        For i As Integer = 0 To ShoeDataBindingSource.Count - 1
            Dim rowData As DataRowView = ShoeDataBindingSource.Item(i)
            If rowData("ShoeID").ToString = IDBox.Text Then
                StockBindingSource.Current("ShoeType") = ShoeDataBindingSource.Current("Type")
                StockBindingSource.Current("Description") = ShoeDataBindingSource.Current("Name")
            End If
        Next
        StockBindingSource.Current("Size") = Size
        StockBindingSource.Current("Stock") = Stock
        StockBindingSource.EndEdit()
        TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
    Else
        For i As Integer = 0 To ShoeDataBindingSource.Count - 1
            Dim rowName As DataRowView = ShoeDataBindingSource.Item(i)
            If rowName("ShoeID").ToString = IDBox.Text And StockBindingSource.Current("Size") = Size Then
                StockBindingSource.Current("Stock") = StockBindingSource.Current("Stock") + Stock
                StockBindingSource.EndEdit()
                TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
                status = True
            End If
            Console.WriteLine(ShoeDataBindingSource.Count)
            stock_check = "Loop"
        Next

        If (Not status And stock_check = "Loop") Then
            Dim no As Integer
            no = StockBindingSource.Count + 1
            StockBindingSource.AddNew()
            StockBindingSource.Current("No") = no
            StockBindingSource.Current("ShoeID") = IDBox.Text
            For i As Integer = 0 To ShoeDataBindingSource.Count - 1
                Dim rowData As DataRowView = ShoeDataBindingSource.Item(i)
                If rowData("ShoeID").ToString = IDBox.Text Then
                    StockBindingSource.Current("ShoeType") = ShoeDataBindingSource.Current("Type")
                    StockBindingSource.Current("Description") = ShoeDataBindingSource.Current("Name")
                End If
            Next
            StockBindingSource.Current("Size") = Size
            StockBindingSource.Current("Stock") = Stock
            StockBindingSource.EndEdit()
            TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
        End If
    End If
End Sub

I enter my data First time First Data enter

Updated same data Second

Add an another data and added the same data again, enter image description here

New Data Updates here enter image description here

Hope you understood the problem. Somebody help please.

StockBindingSource.count does not update. It always shows as 1, unless it is null.


Solution

  • See my comment above in regards to my attempt to post this code into a comment.

    Dim ShoeId As Integer = 1
    Dim ShoeSize As Integer = 15
    Dim Stock As Integer = 1
    Dim thisRow As DataRow = Nothing
    
    Dim dt As New DataTable
    dt.Columns.Add(New DataColumn With
                   {
                       .ColumnName = "Id",
                        .DataType = GetType(Integer),
                        .AutoIncrement = True,
                        .AutoIncrementSeed = 1
                    })
    
    dt.Columns.Add(New DataColumn With
                   {
                        .ColumnName = "ShoeId",
                        .DataType = GetType(Integer),
                        .AutoIncrement = True
                    })
    
    dt.Columns.Add(New DataColumn With
                   {
                       .ColumnName = "Size",
                       .DataType = GetType(Integer)
                   })
    
    dt.Columns.Add(New DataColumn With
                   {
                       .ColumnName = "ShoeType",
                       .DataType = GetType(String)
                   })
    dt.Columns.Add(New DataColumn With
                   {
                       .ColumnName = "Stock",
                       .DataType = GetType(Integer)
                   })
    dt.Rows.Add(New Object() {Nothing, 1, 15, "Shoe", 5})
    
    
    bs.DataSource = dt
    thisRow =
        (
            From t In CType(bs.DataSource, DataTable).AsEnumerable
            Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
            Select t).FirstOrDefault
    
    If thisRow IsNot Nothing Then
        thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
    Else
        ' add
    End If
    
    ShoeId = 2
    ShoeSize = 15
    thisRow =
        (
            From t In CType(bs.DataSource, DataTable).AsEnumerable
            Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
            Select t).FirstOrDefault
    
    If thisRow IsNot Nothing Then
        thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
    Else
        CType(bs.DataSource, DataTable).Rows.Add(New Object() {Nothing, ShoeId, ShoeSize, "Shoe", 5})
    End If
    
    ShoeId = 1
    ShoeSize = 15
    thisRow =
        (
            From t In CType(bs.DataSource, DataTable).AsEnumerable
            Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
            Select t).FirstOrDefault
    
    If thisRow IsNot Nothing Then
        thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
    Else
        CType(bs.DataSource, DataTable).Rows.Add(New Object() {Nothing, ShoeId, ShoeSize, "Shoe", 5})
    End If