Solved

Need help inserting a record into MSSQL Table using ASP VBscript

Posted on 2008-06-26
7
977 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

15 Experts available now in Live!

Get 1:1 Help Now