Solved

Need help inserting a record into MSSQL Table using ASP VBscript

Posted on 2008-06-26
7
978 Views
Last Modified: 2012-06-27
I inadvertently deleted a record from my database and now I need to recreate it. It has to be exact wiuth the original ID number. Here are 2 .asp pages I wrote to accomplish this as well as the error message I get when I try to use them. The first page is a form that gives the second page it's info.

ASP PAGE 1:

 <!--#include virtual="/functions.asp"-->
<!--#include virtual="/admin/header.asp"-->


<%
'********************************************
'* Incoming variables
'********************************************


'********************************************
'* Database setup
'********************************************
set conn=server.createobject("adodb.connection")
conn.open application("dsn_string")
set rs=server.createobject("adodb.recordset")
rs.cursortype=adForwardOnly
rs.cursorlocation=adUseClient
rs.activeConnection=conn
%>

<form action="DivAddwID2.asp">
Division Name: <input type=text name="descr" value=""><br>
Division ID: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type=text name="division_id" value=""><br>
Series ID:&nbsp; &nbsp;&nbsp;&nbsp;<input type=text name="season_id" value=""><br>
Prize Division:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type=text name="isPrizeDivision" value=""><br>
<input type=submit value="Save New Division">
</form>


<!--#include virtual="/admin/footer.asp"-->


ASP PAGE 2:

<!--#include virtual="/functions.asp"-->
<!--#include virtual="/admin/header.asp"-->

<%
'********************************************
'* Incoming variables
'********************************************
descr=request("descr")
division_id=request("division_id")
season_id=request("season_id")
isPrizeDivision=request("isPrizeDivision")

'********************************************
'* Database setup
'********************************************
set conn=server.createobject("adodb.connection")
conn.open application("DSN_String")
set rs=server.createobject("adodb.recordset")
rs.cursortype=adForwardOnly
rs.cursorlocation=adUseClient
rs.activeConnection=conn
sqlString="insert into divisions (descr, division_id, isPrizeDivision, season_id) values (" & descr & ", " & division_id & ", " & isPrizeDivision & ", " & season_id & ")"
rs.open sqlString

response.redirect("DivAddwID.asp")
%>


AND LASTLY, HERE IS THE ERROR:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert explicit value for identity column in table 'Divisions' when IDENTITY_INSERT is set to OFF.
/admin/DivAddwID2.asp, line 23


Also, if I include any text in the first area of the form (descr), I get a different error like this:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]The name 'Film' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
/admin/DivAddwID2.asp, line 23
0
Comment
Question by:bishopandsix
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21877911
It looks like your table has an IDENTITY field (probably the primary key).  You can either omit that value in your insert statement, or run this before the insert

SET IDENTITY_INSERT tablename ON

--do your insert

SET IDENTITY_INSERT tablename OFF

the first step (omit the field) is probably the easiest.
0
 

Author Comment

by:bishopandsix
ID: 21877961
exactly where would I put those lines? Like this:

SET IDENTITY_INSERT tablename ON

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]The name 'Film' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
/admin/DivAddwID2.asp, line 23

rs.open sqlString

SET IDENTITY_INSERT tablename OFF


like that?

omiting the field is not an option as I assume the filed that is locked is the one I specifically need to assign the ID of.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21877988
you could try this:  

sqlString="set identity_insert divisions ON;insert into divisions (descr, division_id, isPrizeDivision, season_id) values (" & descr & ", " & division_id & ", " & isPrizeDivision & ", " & season_id & ") ;set identity_insert divisions OFF"
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

Author Comment

by:bishopandsix
ID: 21878974
Ok, that worked. However, now I get this error when I try to modify another field in this table with an ASP page I've been using forever:

ADODB.Recordset error '800a0e78'

Operation is not allowed when the object is closed.

/admin/divisionmod2.asp, line 29

What do I need to do to put it back how it was?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21879191
Not sure...I would say that your recordset gets closed somewhere, but I can't say for sure.  You're better off posting a new question here (as this is really a different question).  You'll probably get some good VB guys to help you out.
0
 

Author Comment

by:bishopandsix
ID: 21879426
Actually it was my fault. I had commented out a single line of code in this page while I was trying to reverse engineer a way to insert the record I originally posted about. Just a matter of putting it back the way it was. Thanks for the help!

Steve
0
 

Author Closing Comment

by:bishopandsix
ID: 31471126
AWESOME!!! Thanks a ton.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now