Prob Adding and Deleting objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Prob Adding and Deleting objects

  1. #1
    Join Date
    Sep 2008
    Posts
    27

    Prob Adding and Deleting objects

    H1, Using VB Net (2005, Access (2003) in Windows Forms, in my application there are 2 ListBox and a Datagridview. Database and Strong DataSet both have 4 tables and are related; on the dgCurrency_SelectionChanged I do some filtering to supply DataSource for the 2 ListBox, so good so far. My prob start here, upon selecting and element (Item) from left ListBox and adding it to the right ListBox this seem to work (I can see this item has been added to DataBase) but I been trying to delete the item that was selected from the left ListBox, just can do. I been at it for the last 2 weeks, so any help will be greatly appreciated. Thank you

  2. #2
    Join Date
    Sep 2007
    Posts
    86
    If you could post the code you have for deleting from the listbox that is not working then perhaps I could point out where the problem is.
    John Wiese
    ISV Architect Evangelist - Microsoft
    http://blogs.msdn.com/usisvde

  3. #3
    Join Date
    Sep 2008
    Posts
    27
    Hi, hire is part of the code

    Private Sub cmdMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMove.Click
    'Get the current Currency value and the Tax value being Selected.

    Dim Currency_value As String = (tblCurrencyBindingSource.Position + 1).ToString
    Dim Tax_value As String = Taxable.SelectedValue
    Dim SelectedTax As String = Taxable.SelectedItem.ToString
    Dim Num As String = Taxable.SelectedItem.ToString

    'Make the add in the table tblCurTax
    Dim drv As DataRowView = DirectCast(Taxable.SelectedItem, DataRowView)

    Dim nr As DataRow = Me.DataSet1.tblCurTax.NewRow()

    nr("CurID") = drv("CurID")
    nr("TaxID") = drv("TaxID")
    nr("SelectedTax") = drv("SelectedTax")
    nr("Num") = drv("Num")


    Me.DataSet1.tblCurTax.Rows.Add(nr)

    'For Each dr As DataRow In DataSet1.tblCurTax1.Rows
    ' If (dr("SelectedTax") = drv("SelectedTax")) Then
    ' dr.Delete()
    ' End If
    'Next
    'DataSet1.tblCurTax1.AcceptChanges()


    'Filter the DefaultView as it is displayed in the listbox.
    'Me.DataSet1.tblCurTax1.DefaultView.RowFilter = " CurID = " + (tblCurrencyBindingSource.Position + 1).ToString

    'To use the Find method, the DataView must be Sorted.
    'Me.DataSet1.tblCurTax1.DefaultView.Sort = "TaxID"

    'Find the Row Number
    Dim rowindex As Integer = Me.DataSet1.tblCurTax1.DefaultView.Find(drv("TaxID"))

    'Delete the Row
    'DataSet1.tblCurTax1.RemovetblCurTax1Row()
    'Update the database
    Me.Validate()
    Me.tblCurTaxBindingSource.EndEdit()
    Me.tblTaxableBindingSource.EndEdit()
    Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)

    'Call disconnect_reconnect_data_sources()
    'Me.DataSet1.tblCurTax1.Rows(2).Delete()
    'Me.ValidateChildren()
    'Me.tblSelectedTaxBindingSource.EndEdit()
    'Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)
    End Sub

  4. #4
    Join Date
    Sep 2007
    Posts
    86
    Thanks. If I trim out all the code that is commented out then I have this, the actual running code, correct:

    Code:
        Private Sub cmdMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMove.Click
    
            'Get the current Currency value and the Tax value being Selected.
            Dim Currency_value As String = (tblCurrencyBindingSource.Position + 1).ToString
            Dim Tax_value As String = Taxable.SelectedValue
            Dim SelectedTax As String = Taxable.SelectedItem.ToString
            Dim Num As String = Taxable.SelectedItem.ToString
    
            'Make the add in the table tblCurTax
            Dim drv As DataRowView = DirectCast(Taxable.SelectedItem, DataRowView)
            Dim nr As DataRow = Me.DataSet1.tblCurTax.NewRow()
    
            nr("CurID") = drv("CurID")
            nr("TaxID") = drv("TaxID")
            nr("SelectedTax") = drv("SelectedTax")
            nr("Num") = drv("Num")
    
            Me.DataSet1.tblCurTax.Rows.Add(nr)
    
            'Find the Row Number 
            Dim rowindex As Integer = Me.DataSet1.tblCurTax1.DefaultView.Find(drv("TaxID"))
    
            Me.Validate()
            Me.tblCurTaxBindingSource.EndEdit()
            Me.tblTaxableBindingSource.EndEdit()
            Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)
        End Sub
    I don't see in here where you are deleting the entries from the ListBox, or are your ListBoxes DataBound to the tables?
    John Wiese
    ISV Architect Evangelist - Microsoft
    http://blogs.msdn.com/usisvde

  5. #5
    Join Date
    Sep 2008
    Posts
    27
    Hi, you are right, that was previous trial but as you know it din't work
    I have try to send my Ex: but it fail on account exceeded the 100K. Ok I make available My example:
    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'DataSet1.tblCurTax1' table. You can move, or remove it, as needed.
            Me.TblCurTax1TableAdapter.Fill(Me.DataSet1.tblCurTax1)
            'TODO: This line of code loads data into the 'DataSet1.tblCurrency' table. You can move, or remove it, as needed.
            Me.TblCurrencyTableAdapter.Fill(Me.DataSet1.tblCurrency)
            'TODO: This line of code loads data into the 'DataSet1.tblTaxable' table. You can move, or remove it, as needed.
            Me.TblTaxableTableAdapter.Fill(Me.DataSet1.tblTaxable)
            'TODO: This line of code loads data into the 'DataSet1.tblCurTax' table. You can move, or remove it, as needed.
            Me.TblCurTaxTableAdapter.Fill(Me.DataSet1.tblCurTax)
    
            Taxable.ClearSelected()
            TaxExempt.ClearSelected()
    
            Taxable.DisplayMember = "SelectedTax"
            Taxable.ValueMember = "CurID"
            Taxable.DataSource = tblCurTax1BindingSource
    
            TaxExempt.DisplayMember = "SelectedTax"
            TaxExempt.ValueMember = "CurID"
            TaxExempt.DataSource = tblCurTaxBindingSource
    
        End Sub
    
        Private Sub dgCurrency_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgCurrency.SelectionChanged
            'Allow Taxable Objects linked to Currency to be filter.
            tblCurTax1BindingSource.Filter = " CurID = " + (tblCurrencyBindingSource.Position + 1).ToString
            tblCurTaxBindingSource.Filter = " CurID = " + (tblCurrencyBindingSource.Position + 1).ToString
    
        End Sub
    
        Private Sub cmdMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMove.Click
            'Get the current Currency value and the Tax value being Selected.
    
            Dim Currency_value As String = (tblCurrencyBindingSource.Position + 1).ToString
            Dim Tax_value As String = Taxable.SelectedValue
            Dim SelectedTax As String = Taxable.SelectedItem.ToString
            Dim Num As String = Taxable.SelectedItem.ToString
    
            'Make the add in the table tblCurTax
            Dim drv As DataRowView = DirectCast(Taxable.SelectedItem, DataRowView)
    
            Dim nr As DataRow = Me.DataSet1.tblCurTax.NewRow()
    
            nr("CurID") = drv("CurID")
            nr("TaxID") = drv("TaxID")
            nr("SelectedTax") = drv("SelectedTax")
            nr("Num") = drv("Num")
            
    
            Me.DataSet1.tblCurTax.Rows.Add(nr)
    
            'For Each dr As DataRow In DataSet1.tblCurTax1.Rows
            '    If (dr("SelectedTax") = drv("SelectedTax")) Then
            '        dr.Delete()
            '    End If
            'Next
            'DataSet1.tblCurTax1.AcceptChanges()
    
    
            'Filter the DefaultView as it is displayed in the listbox. 
            'Me.DataSet1.tblCurTax1.DefaultView.RowFilter = " CurID = " + (tblCurrencyBindingSource.Position + 1).ToString
    
            'To use the Find method, the DataView must be Sorted. 
            'Me.DataSet1.tblCurTax1.DefaultView.Sort = "TaxID"
    
            'Find the Row Number 
            Dim rowindex As Integer = Me.DataSet1.tblCurTax1.DefaultView.Find(drv("TaxID"))
    
            'Delete the Row 
            'DataSet1.tblCurTax1.RemovetblCurTax1Row()
            'Update the database  
            Me.Validate()
            Me.tblCurTaxBindingSource.EndEdit()
            Me.tblTaxableBindingSource.EndEdit()
            Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)
    
            'Call disconnect_reconnect_data_sources()
            'Me.DataSet1.tblCurTax1.Rows(2).Delete()
            'Me.ValidateChildren()
            'Me.tblSelectedTaxBindingSource.EndEdit()
            'Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)
        End Sub
    
        Private Sub cmdMoveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMoveAll.Click
    
        End Sub
    
        Private Sub cmdBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            'Get the current Currency value and the Tax value being Selected.
    
            Dim Currency_value As String = (tblCurrencyBindingSource.Position + 1).ToString
            Dim Tax_value As String = TaxExempt.SelectedValue
            Dim SelectedTax As String = TaxExempt.SelectedItem.ToString
            Dim Num As String = TaxExempt.SelectedItem.ToString
    
            'Make the add in the table tblCurTax1
            Dim drv As DataRowView = DirectCast(TaxExempt.SelectedItem, DataRowView)
    
            Dim nr As DataRow = Me.DataSet1.tblCurTax1.NewRow()
    
            nr("CurID") = drv("CurID")
            nr("TaxID") = drv("TaxID")
            nr("SelectedTax") = drv("SelectedTax")
            nr("Num") = drv("Num")
    
    
            Me.DataSet1.tblCurTax1.Rows.Add(nr)
    
            'For Each dr As DataRow In DataSet1.tblCurTax1.Rows
            '    If (dr("SelectedTax") = drv("SelectedTax")) Then
            '        dr.Delete()
            '    End If
            'Next
            'DataSet1.tblCurTax1.AcceptChanges()
    
    
            'Filter the DefaultView as it is displayed in the listbox. 
            'Me.DataSet1.tblCurTax1.DefaultView.RowFilter = " CurID = " + (tblCurrencyBindingSource.Position + 1).ToString
    
            'To use the Find method, the DataView must be Sorted. 
            'Me.DataSet1.tblCurTax1.DefaultView.Sort = "TaxID"
    
            'Find the Row Number 
            Dim rowindex As Integer = Me.DataSet1.tblCurTax1.DefaultView.Find(drv("TaxID"))
    
            'Delete the Row 
            'DataSet1.tblTaxable(1).Delete()
    
            'Update the database 
            Me.Validate()
            Me.tblCurTax1BindingSource.EndEdit()
            Me.tblTaxableBindingSource.EndEdit()
            Me.TblCurTax1TableAdapter.Update(Me.DataSet1.tblCurTax1)
    
            'Call disconnect_reconnect_data_sources()
            'Me.DataSet1.tblCurTax1.Rows(2).Delete()
            'Me.ValidateChildren()
            'Me.tblSelectedTaxBindingSource.EndEdit()
            'Me.TblCurTaxTableAdapter.Update(Me.DataSet1.tblCurTax)
        End Sub
    
        Private Sub cmdBackAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackAll.Click
    
        End Sub
    
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Me.Close()
        End Sub
    
    End Class
    Last edited by Hack; 01-15-2009 at 07:42 AM. Reason: Added Code Tags

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What exceeded 100K?

    What is the exact error message?

    (BTW: I edited your post and added [code]your code goes here[/code] tags to make it easier to read. )
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Sep 2008
    Posts
    27
    Hi to you this morning. About the 100K, I wanted to Zip you my code but it seem that you have a limit for download of a 100K.Now about my code Ihave no problem to un-comment my code, (BTW: I edited your post and added
    Code:
    your code goes here
    tags to make it easier to read. ) when you say you edit my code, is it by removing the not use comment codes like I said this I can eaisly do. As far for the error message I don't have any, it is just that I don't know, (When you ask to see my code, all the comment code was previous trial that did not work) so you see my problem now how to delete the item previous selected just after having add that item to the DataSet and also to the Database aned do the necessary updating DataSet and DataBase, hope being clear enough and still hope you can help me with my problems and this I thank you

Similar Threads

  1. Replies: 0
    Last Post: 04-11-2003, 06:27 AM
  2. Adding and deleting records (dnagel)
    By Jamal in forum VB Classic
    Replies: 2
    Last Post: 02-25-2003, 08:21 AM
  3. Adding and deleting records
    By Jamal in forum VB Classic
    Replies: 1
    Last Post: 02-20-2003, 05:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center