Record locking in ASP

How can I fix this is asp

Microsoft OLE DB Provider for SQL Server error '80004005'

Transaction (Process ID 74) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

/mypage.asp, line 87


This is the code
<%
 sqlstr2 = "IF NOT EXISTS "&_
          "(SELECT site_id FROM Mcounter WHERE site_id = " & RS("site_id") & " AND type_id = " & RS("type_id") & " AND year_click = " & varYear & ") "&_
                    "BEGIN " &_
                         "INSERT INTO mCounter (company_id, site_id, type_id, year_click, " & varMonth & ") "&_
                         "VALUES "&_
                         "(" & RS("company_id") & ", " & RS("site_id") & ", " & RS("type_id") & ", " & varYear & ", 1) "&_
                    "END " &_
               "ELSE " &_
                    "BEGIN " &_
                         "UPDATE Mcounter SET " & varMonth & " = " & varMonth & " + 1 WHERE site_id = " & RS("site_id") & " AND type_id = " & RS("type_id") & " AND year_click = " & varYear & " "&_
                    "END"    
                   
     
     Myconn.Execute(mysql)  'this code is generating the eror
%>


diamilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

apolloisCommented:
Convert to stored procedure.

More secure, much better performance, easier to test/debug.

Best Regards,
apollois
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chisholmdCommented:
Ya I completly agree. Allot of people use dynamic sql to generate simply select, insert, or update queries.  But your dynamically generating a whole procedure...would be much better to write the stored procedure and pass the parameters in.

not looking for points just trying to reinforce appollois suggestion.
0
apolloisCommented:
diamil,

Thanks for selecting my comment as the answer.  But why the grade of "B"?

The recommendation I gave you, confirmed by chisholmd, is probably the best thing you can do to solve your problem.  I realize that it may not have been the answer you were looking for, but none-the-less, it is a sound answer.

If you have any related questions, I will be glad to help you.

Please see Tip #11 - Grade quickly and fairly
http://cd-eepages.fateback.com/asking.html

If you wish to change the grade, you can post a zero-point question with Community Support (http://www.experts-exchange.com/Community_Support/).  Include a link to this question, and the new grade.

Best Regards,
apollois
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

diamilAuthor Commented:
I gave B, because I was waiting for something that will help me use a transaction, sorry for the B.

Thanks
0
apolloisCommented:
diamil,

Well, if I didn't answer your question, then you should not have selected my comment as answer.

It would have been better if you would have posted your above comment before selecting the comment.

So, I'd recommend that you ask CS to reopen the question so we can give you the proper help.

Having said that, can you convert your SQL to a stored procedure?

Best Regards,
apollois
0
diamilAuthor Commented:
I did not implemente the solution using a stored procedure (I do recognize that using a stored procedure is better and more reliable). If you look closely at the query I posted the logic was wrong. Since we have more updates than adding a new record, it was better to create two separate queries


If user exist then
update query
end if

if user does not exist then
add a new record query
end if

this logic was able to solve my problem.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.