Solved

Need help inserting a record into MSSQL Table using ASP VBscript

Posted on 2008-06-26
7
979 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

785 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