Results 1 to 27 of 27

Thread: my program deletes the first record in my database...[RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Talking my program deletes the first record in my database...[RESOLVED]

    hi!

    I've been posting here questions and I'm glad to have found the answers from all of you. I think you're all good and very helpful to beginners like me...

    Well, I just want to ask for some help...I have a form with a master/detail relationship...what I want to do is to update and delete records. I have started coding my delete button. Thought it was working well but I noticed just this mornng that it deletes the first record even if I navigated to the record that I wanted to delete. Please try to help me! I need to finish this soon...I hope someone will assist me on this!

    Thanks in advance!
    Last edited by siomai; Aug 19th, 2004 at 03:07 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    OK, and what code do you have there to delete the record?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hi mendhak!

    VB Code:
    1. Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
    2.         Dim i As String
    3.         Dim dr As DataRow
    4.         Dim cmd As String
    5.         Dim cbuild As System.Data.OleDb.OleDbCommandBuilder
    6.  
    7.         Try
    8.             BindingContext(ds, "QuoteDoc").RemoveAt(BindingContext(ds, "QuoteDoc").Position)
    9.             cbuild = New System.Data.OleDb.OleDbCommandBuilder(daDoc)
    10.             daDoc.DeleteCommand = cbuild.GetDeleteCommand
    11.             daDoc.Update(ds, "QuoteDoc")
    12.  
    13.             BindingContext(ds, "QuoteTran").RemoveAt(BindingContext(ds, "QuoteTran").Position)
    14.             ds.Tables("QuoteTran").Rows(0).Delete()
    15.             Me.BindingContext(ds, "QuoteTran").EndCurrentEdit()
    16.             cbuild = New System.Data.OleDb.OleDbCommandBuilder(daTran)
    17.             daTran.DeleteCommand = cbuild.GetDeleteCommand
    18.  
    19.             daTran.Update(ds, "QuoteTran")
    20.             MsgBox("Record deleted.", MsgBoxStyle.OKOnly, "Delete")
    21.         Catch ex As Exception
    22.         End Try
    23.     End Sub

    If you can remember, you helped me on this and told you it's working already but to my surprise, it deletes the first record of my database.

    This only happens upon loading then I go straight to my Find button then navigate through my First,Next,Previous and Last buttons but if from saving a new record it works fine.

    I hope I did not confuse you.

    If it's not too much to ask and if you're just interested, please take a look at my program that I attached here. I'm very much willing to accept suggestions and recommendations from all of you!
    Attached Files Attached Files

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hello? anybody? Please help!

  5. #5
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    Originally posted by siomai
    VB Code:
    1. ds.Tables("QuoteTran").Rows(0).Delete()
    This is because here you are telling it to delete the first row in the table. Find the row you want to delete and then call its delete method.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    thanks for replying!

    I'm really new with programming so my logic still sucks...could you somehow tell me how to instruct my program to delete the current/selected record? I have a master/detail relationship and what I do is I fill the form (textboxes for my master and a datagrid for my details) then navigate to the record I want to delete.

    Thank you so much!

  7. #7
    Addicted Member
    Join Date
    Aug 2003
    Posts
    153
    I haven't really used datagrids but try this

    VB Code:
    1. Dim i As Integer
    2.  
    3. For i = 0 to ds.Tables("QuoteTran").Rows.Count - 1
    4.  
    5.     If grid.IsSelected(i) = True Then
    6.          'Found the selected row
    7.          dsTables("QuoteTran").Rows(i).Delete
    8.          Exit For
    9.     End If
    10.  
    11. Next

    I am not sure how well this will work for datagrids who have their order sorted and swapped around. Some of the others may of had more experience with datagrids.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    thanks again for replying!

    I have tried your code, but it still deletes the first record in my details.. but thanks for trying! I appreciate it so much!

    I hope someone could help me..I really need it!

  9. #9
    Member
    Join Date
    Mar 2004
    Posts
    53
    VB Code:
    1. If (Me.BindingContext(ds, "QuoteDoc").Count > 0) Then 'added this to make sure there is at leaste 1 row to delete
    2.       Me.BindingContext(ds, "QuoteDoc").RemoveAt(Me.BindingContext(ds, "QuoteDoc").Position)
    3. End If
    4. 'Then update your dataset

    that's what I use all the time, works fine for me

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    thanks for replying!

    then I'll just repeat the code for the details table? or it will automatically delete the details in my grid? I have a master/detail form with textboxes for the master and a datagrid for the details.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    my program still deletes the first record. I really can't make this work alone! please help me! I'm desperate!
    Last edited by siomai; Aug 15th, 2004 at 10:36 PM.

  12. #12
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    what's ExpertSearch? I tried to run your program but you didn't include something called ExpertSearch.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    ExpertSearch is a program I downloaded from the internet.Kindly add the reference ExpertSearch to make my program work...please bear with my code. I know it's really ugly and long...i'm no programmer til now...
    that's why i'm always posting here hoping someone could help me. Anyway, i attached the dll file of ExpertSearch and can be found in the bin folder of my program. Thanks for taking time in looking at my program andy! I'll wait for your suggestions. I'll really appreciate it!

  14. #14
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    this?
    VB Code:
    1. Dim cn As New SqlConnection("user id=sa;password=password;initial catalog=northwind")
    2.    Dim da As New SqlDataAdapter()
    3.    Dim dt As New DataTable("territories")
    4.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.       da.SelectCommand = New SqlCommand("select * from territories", cn)
    6.       da.Fill(dt)
    7.  
    8.       DataGrid1.DataSource = dt
    9.    End Sub
    10.  
    11.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.       If Not r Is Nothing Then
    13.          r.Delete()
    14.          r = Nothing
    15.          Dim scb As New SqlCommandBuilder(da)
    16.          da.Update(dt)
    17.       End If
    18.    End Sub
    19.  
    20.    Dim r As DataRow
    21.    Private Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
    22.       r = dt.Rows(DataGrid1.CurrentCell.RowNumber)
    23.    End Sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    Mabuhay brown monkey! Thanks for replying to my post...i'm kind of giving up on this program already! I'm not really a good programmer (yet! hehe) but could you explain to me what your code does?(if you don't mind) I mean I know it deletes records but how?

  16. #16
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i'm a cebuano so "mabuhi ka siomai". hehehe. my program deletes every row clicked on a grid. say i click on the third row and press the delete button, it deletes the third row on a table buffered and on the database as well.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    I see...but could you help me in deleting my record with master/detail relationship? this is the code I have and it deletes the first record in my database only. Please please help me with this one! I'll be forever grateful!
    Code:
       Dim cbuild As System.Data.OleDb.OleDbCommandBuilder
            Try
    
                BindingContext(ds, "QuoteDoc").RemoveAt(BindingContext(ds, "QuoteDoc").Position)
                cbuild = New System.Data.OleDb.OleDbCommandBuilder(daDoc)
                daDoc.DeleteCommand = cbuild.GetDeleteCommand
                daDoc.Update(ds, "QuoteDoc")
                BindingContext(ds, "QuoteTran").RemoveAt(BindingContext(ds, "QuoteTran").Position)
                cbuild = New System.Data.OleDb.OleDbCommandBuilder(daTran)
                daTran.DeleteCommand = cbuild.GetDeleteCommand
                daTran.Update(ds, "Quotetran")
                ds.AcceptChanges()
                daTran.DeleteCommand.Connection.Close()
    
                MsgBox("Record deleted.", MsgBoxStyle.OKOnly, "Update")
            Catch ex As Exception
            End Try
    
        End Sub
    I have already created the relation in my find button click event...hope you could help me. Thanks in advance kababayan!

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Unhappy

    anyone? can anyone help please?

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    i got my problem....now how to point my program to select the current record...

    I read about bindingcontext and it is by default, pointing to the first record (0 in index) that's why my program deletes the first record.

    Now what I want to know now is how I could point my program to select the current record displayed in my form and point that record to my database so I can delete it. (help me code this please)

    Hope the vb.net gurus out there would help me. Many thanks!

  20. #20
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    VB Code:
    1. datagrid1.currentrowindex

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    Hi!
    I'm not just deleting the record in my datagrid. I want to delete the current record displayed in my form. I understand that when you have a relation set, it automatically deletes the details or the child records of that current mother record. This works fine with me, it deletes my mother and child records but the problem is it doesn't delete the currently displayed record. Instead, it deletes the first record in my database.
    What does this line point to...(i mean what record in my database?)

    Code:
    Me.BindingContext(ds, "QuoteDoc").RemoveAt(Me.BindingContext(ds, "QuoteDoc").Position)
    Correct me if I'm wrong but I think this is pointing to the currently displayed record!!! I've been trying to solve this glitch for a week now but to no luck, the glitch still exists....please help me.

    To anyone who has done program with a master/detail form, please share your knowledge to me. I'll be very very thankful if I'd be able to solve this problem.Thanks in advance.

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Unhappy quite desperate now...

    please help me! I know you have other stuff to do but a simple suggestion could make a difference. I really don't know where to go from here. Please please help me!

  23. #23
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    does setting the cascade delete on your sql do the trick? i mean it's simple but quiet good though.

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hi! thanks for replying to my long thread...
    I'm no expert in programming. Actually this is my first program ever (well i did the calculator thingy...). Anyway, could you show me how? I just want to be able to delete a master/detail (parent/child) record. What my current program does is it deletes the first record in my database though i displayed another record in my form. It seems that my code now doesn't point to the record that i actually want to delete.

  25. #25
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    never tried your code though and i don't do binding. and i'm a newbie. i tried purchase requisition (pr) thing. the master detail relation but i don't do the relation object. here's what i do, to sql server 2000, design table. say the pr master. click the relationships. then new relation. mostly the pr master number relates the pr details number. there should be something like a checkbox underneath with text "Cascade Delete Related Records". check this mate. this might help you. if you want an example i'll try to have an example but quiet busy now. perhaps i can post sample tomorrow. the pr...
    Last edited by brown monkey; Aug 19th, 2004 at 01:24 AM.

  26. #26
    New Member
    Join Date
    Aug 2004
    Posts
    6
    what you could try, which i often do to debug errors i get like this, is simply display the position thats selected befopre your delete command. i havent really ever used datasets, but try somehting like this:

    Code:
       Dim cbuild As System.Data.OleDb.OleDbCommandBuilder
            Try
                 ' check position of current bindingcontext
                msgbox(BindingContext(ds,"QuoteDoc").Position)
                BindingContext(ds, "QuoteDoc").RemoveAt(BindingContext(ds, "QuoteDoc").Position)
                cbuild = New System.Data.OleDb.OleDbCommandBuilder(daDoc)
                daDoc.DeleteCommand = cbuild.GetDeleteCommand
                daDoc.Update(ds, "QuoteDoc")
                BindingContext(ds, "QuoteTran").RemoveAt(BindingContext(ds, "QuoteTran").Position)
                cbuild = New System.Data.OleDb.OleDbCommandBuilder(daTran)
                daTran.DeleteCommand = cbuild.GetDeleteCommand
                daTran.Update(ds, "Quotetran")
                ds.AcceptChanges()
                daTran.DeleteCommand.Connection.Close()
    
                MsgBox("Record deleted.", MsgBoxStyle.OKOnly, "Update")
            Catch ex As Exception
            End Try
    
        End Sub
    see what im doing there? that way you can identify whats causing your problem, by checking different values and making sure they are set correctly.
    |dr. ludicrous, i turn into an octopus...|

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Thumbs up Thanks a lot to all of you!!!

    my problem is solved! I've found a link and tried the code it showed. I used parameters to make my program select the record/s it has to delete...and it worked!

    But without you guys...I would've given up programming!
    There's more to come from me..haha! this program isn't done yet! Please watch out for my queries. Again thanks a lot!
    VB forums.com people are the best!

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