Solved

Need help inserting a record into MSSQL Table using ASP VBscript

Posted on 2008-06-26
7
981 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
[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 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

696 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