Results 1 to 7 of 7

Thread: Add textbox value to datatable multiple times

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    5

    Exclamation Add textbox value to datatable multiple times

    Here is my code:
    HTML Code:
    Dim num As Integer
    num = Me.txtCopies.Text
    
    If num = 0 Then
      MessageBox.Show("The number of copies cannot be under 1.", "Popcorn Movie Rentals Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
      Me.txtCopies.Select()
      Me.txtCopies.Focus()
    Else
      Me.PmrDataSet.Movies.Rows.Add(movie)
    End If
    My textbox (txtCopies) default value is 1 so naturally, as it is coded above, the datarow is added once into the Movies Table. But what I need to know is how to code if the txtCopies field has a greater integer value.

    Thx for all the help in advance!

    LadySylvia

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Add textbox value to datatable multiple times

    try this:

    vb Code:
    1. Dim num As Integer
    2. integer.tryparse(Me.txtCopies.Text, num)
    3.  
    4. If num = 0 Then
    5.     MessageBox.Show("The number of copies cannot be under 1.", "Popcorn Movie Rentals Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    6.     Me.txtCopies.Select()
    7.     Me.txtCopies.Focus()
    8. Else
    9.     for x as integer = 1 to num
    10.         Me.PmrDataSet.Movies.Rows.Add(movie)
    11.     next
    12. End If

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    5

    Re: Add textbox value to datatable multiple times

    Unfortunately no, it didn't work, here's the error message:

    ArgumentException was unhandled
    This row already belongs to this table.


    No idea what that means!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Add textbox value to datatable multiple times

    what is movie? is it a datarow?

    try movie.clone instead of movie

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    5

    Re: Add textbox value to datatable multiple times

    yes, movie is a datarow.
    i was told to arrange the loop so that the row is only handled once, then found and copied after.
    the loop is written after the code i posted above.
    think that would work?

    here's the current loop:

    vb Code:
    1. Me.MoviesTableAdapter.Update(Me.PmrDataSet)
    2.  
    3.             Dim findmovies As DataRow
    4.             Me.PmrDataSet.Members.PrimaryKey = New DataColumn() {Me.PmrDataSet.Movies.Columns("Title")}
    5.             findmovies = Me.PmrDataSet.Members.Rows.Find(Me.txtTitle.Text)
    6.  
    7.             For Each findmovies In Me.PmrDataSet.Movies.Rows
    8.                 If findmovies.Item("Title") = Me.txtTitle.Text Then
    9.                     movieIdsfrm.lbxHidden.Items.Add(findmovies.Item("MovieId"))
    10.                 End If
    11.             Next
    12.  
    13.             movieIdsfrm.Show()

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Add textbox value to datatable multiple times

    why do you need to add multiples of the same datarow to your datatable?
    looking at your code in post#5 it looks like you want to add the movie MovieId to a listbox?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Add textbox value to datatable multiple times

    try this:

    vb Code:
    1. Me.MoviesTableAdapter.Update(Me.PmrDataSet)
    2.  
    3. Dim findmovies As DataRow
    4. Me.PmrDataSet.Members.PrimaryKey = New DataColumn() {Me.PmrDataSet.Movies.Columns("Title")}
    5. findmovies = Me.PmrDataSet.Members.Rows.Find(Me.txtTitle.Text)
    6.  
    7. movieIdsfrm.Show()
    8.  
    9. For Each movie In findmovies
    10.     If movie.Item("Title") = Me.txtTitle.Text Then
    11.         movieIdsfrm.lbxHidden.Items.Add(movie.Item("MovieId"))
    12.     End If
    13. Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width