Solved

Can't use recordset .AddNew and rs.State raise error: Operation is not allowed in this context. ?

Posted on 2008-11-03
7
629 Views
Last Modified: 2011-10-19
Hi,
The first trouble is IIS doesn't dislay script language error msg with which line raised error ?
And next.
I dun't know why last time this code worked with MS SQL 2008 Enterprise. But i changed to 2005 Express than everything dump.
For below code why :
- I got no error but addnew doesn't effect
- This code was worked last time with SQL 2008 Ent ???
And ... this code will raise error at rs.State of Class_Terminate of clsProduct (please ref in file)

Please help me to solve addnew that i can insert to databse.

Thank you
Public Function AddNew (objProduct)			
'			On Error Resume Next
			p_ErrorMsg = ""				
			
			If IsObject(objProduct)  Then
				' --------------- Validating ---------------
				p_ErrorMsg = objProduct.ErrorMsg
				' --------------- Validating ---------------																
			End If
			
			If p_ErrorMsg = "" Then ' Everything is OK 
				rs.Open "eshop_products", objConn, adOpenKeyset, adLockOptimistic, adCmdTable
				If rs.State = adStateOpen Then
					rs.AddNew							
						rs("product_name") = CStr(objProduct.ProductName)
						rs("category_id") = CInt(objProduct.Category.CategoryID)
						If HaveValue(objProduct.Colour) Then rs("product_colour") = CStr(objProduct.Colour)
						If HaveValue(objProduct.Image) Then rs("product_image") = CStr(objProduct.Image)
						rs("product_price") = CInt(objProduct.Price)
						If HaveValue(objProduct.SalePrice) Then rs("product_saleprice") = CInt(objProduct.SalePrice)
						If HaveValue(objProduct.ProductDescription) Then rs("product_description") = CStr(objProduct.ProductDescription)
						rs("product_featured") = CBool(objProduct.Featured)			
					rs.Update
					If rs("product_id") <> 0 Then ' OK					
						AddNew = rs("product_id")
					Else
						p_ErrorMsg = " Can't Add new Product. Something wrong"
					End If				
				End If				
			End If
			If rs.State = adStateOpen Then rs.Close
			If Err.Number > 0 Then p_ErrorMsg = Err.Description	
		End Function

Open in new window

Product.asp.txt
0
Comment
Question by:BrendaMcMahon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 41

Expert Comment

by:graye
ID: 22867767
Put an "else" clause in there to record what's going on if the rs.Open fails.
Did you remember to add "\SQLEXPRESS" to the end of the server name in the connection string?
0
 

Author Comment

by:BrendaMcMahon
ID: 22867951
My conn below.

And i'm sure that rs.Open is ok because i tried to use response.write (something) inside adStateOpen and it's work.



strConn = "Provider=SQLOLEDB;Persist Security Info=False;Data Source=MONSTER\SQLEXPRESS;Initial Catalog=eshop;User Id=sa;"

Open in new window

0
 
LVL 41

Expert Comment

by:graye
ID: 22868884
I'd check it anyway.... if the connection failed, then you wouldn't get any error message and the insert wouldn't run (which sounds a lot like what you've described)
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:BrendaMcMahon
ID: 22872251
Thank you,
But ... the problem still over there. I dont know why the Class_Terminate raised while i'm still not destroy the object.

Here is my used code to test AddNew.

The event Class_Terminate raised right after call AddNew without nothing ???

And these code was worked with SQL Server 2008 Ent, only raise error with SQL 2005 !!!
<% Option Explicit %>
 
<!-- #include file="includes/database.asp" -->
<!-- #include file="includes/validating.asp" -->
<!-- #include file="classes/Product.asp" -->
<!-- #include file="classes/clsCategory.asp" -->
 
 
<%
Dim objProduct
Set objProduct = New clsProduct
Dim objProducts
Set objProducts = New clsProducts
 
With objProduct
	.ProductName = "316ti SE 3dr"
	.Price = 5457
	.Category.CategoryID = 2
	.ProductID = 9	
End With
response.Write(objProducts.AddNew (objProduct))
response.Write(objProducts.ErrorMsg)
%>

Open in new window

iis-productasp.png
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 22872564
Well, let's try this to begin with....

Inside your Class_Terminate do something like this:

if rs Is Nothing then
....
end If
0
 

Author Comment

by:BrendaMcMahon
ID: 22873239
1. Thank you. I almost found what's wrong. It's by rs with wrong field name.
And because in database file have On Error Resume so system doesn't raise error.
2. Anyways, thank you for give me idea with check on rs for each case.

So i close this question and give points for you.
0
 

Author Closing Comment

by:BrendaMcMahon
ID: 31512711
Thank you for fast and nice help
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Html form and modal / img src -problem 3 59
Parsing this XML works but the other one doesn't 9 35
SP result not being displayed 5 69
JQuery DataTable Functionality 8 54
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question