CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Error due to Locking Tables

    I am having 2 similar forms with a button. Once the button is pressed, a set of generated data is to be inserted in to a table.
    If 2 types of data is to be inserted by the 2 programs, what is the way of avoid crashing one program due to locking of the table?

    It would be better if the answer doesn't contain any error traping mechanism.
    option Explicit
    Dim con as new ADODB.Connection

    private Sub Command1_Click()
    Dim i as Integer

    '"d" is the ODBC name, corresponds to the database having table t1, with fields f1 & f2
    con.Open "d"
    ' this part is in one project
    for i = 1 to 5000
    con.Execute "Insert into t1 (f1,f2) values(" & i " , 'qqqq')"
    next i

    ' this part is in the other project
    ' for i = 5001 to 10000
    ' con.Execute "Insert into t1 (f1,f3) values("& i & " , 'xxxx')"
    ' next i

    con.Close
    End Sub


    Regards
    Srinika

    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Error due to Locking Tables


    Without error handling it may be quite impossible.
    However, transactions may help (even if you may need an "on error resume next"
    statement):

    con.Errors.Clear
    con.begintrans
    for i = 1 to 5000
    con.Execute "Insert into t1 (f1,f2) values(" & i " , 'qqqq')"
    next i
    if con.errors.count>0 then
    con.rollbackTrans
    else
    con.CommitTrans
    end if
    ' this part is in the other project
    'con.Errors.Clear
    'con.begintrans

    'for i = 5001 to 10000
    'con.Execute "Insert into t1 (f1,f3) values("& i & " , 'xxxx')"
    'next i
    'if con.errors.count>0 then
    ' con.rollbackTrans
    'else
    ' con.CommitTrans
    'end if





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured