473,473 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ASP automatic Insert

23 New Member
I want an automatic insert.
I've an ASP page with more than 50 fields (form) and I want to an insert all that information in a table in SQL server 2005.

Now, I've statments for insert BUT I need to create en automatic insert, because sometimes I need to add new fields or remove some fields (from HTML form page), and I dont want to modify the insert statment every time I add a new field

I dont know if I explained my idea.

I really appreciate any help

Thank you
Jul 19 '07 #1
16 1764
jhardman
3,406 Recognized Expert Specialist
the best way to do this is to make the form input names exactly like the db field names. Then instead of using an "Insert" statement, use "SELECT" and "addnew":
Expand|Select|Wrap|Line Numbers
  1. dim query, x
  2. set objRS = server.creatObject("adodb.recordset")
  3. query = "SELECT * FROM userTable"
  4. objRS.open query, objConn
  5.  
  6. objRS.addNew
  7. for each x in objRS.fields
  8.    objRS(x) = request.form(x)
  9. next
  10. objRS.update
This just goes through each field of the db and looks for an input with that name to put in the db. x in this case is a string, so you could do multiple rows in the db at the same time with this method. For example if you had fields named name, address, and phNum, you could put inputs named name1, name2, name3 etc, then put the addnew and loop inside another loop like this:
Expand|Select|Wrap|Line Numbers
  1. dim query, x, y
  2. set objRS = server.creatObject("adodb.recordset")
  3. query = "SELECT * FROM userTable"
  4. objRS.open query, objConn
  5.  
  6. for y = 0 to 3
  7.    if name(y) <> "" then
  8.       objRS.addNew
  9.       for each x in objRS.fields
  10.          objRS(x) = request.form(x&y)
  11.       next
  12.    end if
  13. next
  14. objRS.update
Does this work for you? I generally dislike using insert statements, I think "select" is more versatile.

Jared
Jul 20 '07 #2
jhardman
3,406 Recognized Expert Specialist
Now that I think about it, this might give you an error if there is a field in the db that doesn't come from the form (like an autonumber), so you should probably say:
Expand|Select|Wrap|Line Numbers
  1.       for each x in objRS.fields
  2.          if request.form(x) <> "" then
  3.             objRS(x) = request.form(x)
  4.          end if
  5.       next
Jared
Jul 20 '07 #3
nkoriginal
23 New Member
THANK YOU SO MUCH!! I really appreciate your help guys!

NIKO
Jul 20 '07 #4
nkoriginal
23 New Member
Hi again:

I tried to use this sentence but it's not working, I would like to know if you can help me with this code.

I created an asp page
then I created inside it a html form

and I put this code

<%
'set objRS = server.createObject("adodb.recordset")
'Set loConn = CreateObject("ADODB.Connection")

dim query, x

Set loConn = CreateObject("ADODB.Connection")
loConn.Open(MM_mackoul_STRING)

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = MM_mackoul_STRING
objRS.CursorType = 0
objRS.CursorLocation = 2
objRS.LockType = 3

query = "SELECT * FROM t1"
objRS.open query, loConn

objRS.addNew
for each x in objRS.fields
objRS(x) = request.form(x)
next
objRS.update
%>
But I've an error when I try to submit it. this is error:

Microsoft OLE DB Provider for SQL Server error '80004005'

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

Could you help me please?
Thank you
Aug 9 '07 #5
nkoriginal
23 New Member
Now that I think about it, this might give you an error if there is a field in the db that doesn't come from the form (like an autonumber), so you should probably say:
Expand|Select|Wrap|Line Numbers
  1.       for each x in objRS.fields
  2.          if request.form(x) <> "" then
  3.             objRS(x) = request.form(x)
  4.          end if
  5.       next
Jared
Hi again:

I tried to use this sentence but it's not working, I would like to know if you can help me with this code.

I created an asp page
then I created inside it a html form

and I put this code
Expand|Select|Wrap|Line Numbers
  1. <%
  2. 'set objRS = server.createObject("adodb.recordset")
  3. 'Set loConn = CreateObject("ADODB.Connection")
  4.  
  5. dim query, x
  6.  
  7. Set loConn = CreateObject("ADODB.Connection")
  8. loConn.Open(MM_mackoul_STRING)
  9.  
  10. Set objRS = Server.CreateObject("ADODB.Recordset")
  11. objRS.ActiveConnection = MM_mackoul_STRING
  12. objRS.CursorType = 0
  13. objRS.CursorLocation = 2
  14. objRS.LockType = 3
  15.  
  16. query = "SELECT * FROM t1"
  17. objRS.open query, loConn
  18.  
  19. objRS.addNew
  20. for each x in objRS.fields
  21. objRS(x) = request.form(x)
  22. next
  23. objRS.update
  24. %>
But I've an error when I try to submit it. this is error:

Microsoft OLE DB Provider for SQL Server error '80004005'

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

Could you help me please?
Thank you
Aug 10 '07 #6
jhardman
3,406 Recognized Expert Specialist
what line gives you the error?

Jared
Aug 10 '07 #7
nkoriginal
23 New Member
what line gives you the error?

Jared
Thank Jared for answer again my question.

The error is this line

objRS(x) = request.form(x)

Do you know why?
Thank you
Aug 10 '07 #8
jhardman
3,406 Recognized Expert Specialist
Do you know why?
not really. the error is pretty vague. it is possible that the db is in a read-only mode (this line makes changes to the recordset, although I think if it is in read-only mode you get an error on the update line), so let's try making sure we have an edit mode. locktype should be 3 and sometimes it seems like I get errors unless I make the cursortype = 2. Try that and see if it works.

Jared
Aug 10 '07 #9
jhardman
3,406 Recognized Expert Specialist
if that doesn't work, you could also try:
Expand|Select|Wrap|Line Numbers
  1. for each x in objRS.fields
  2.    objRS(x.name) = request.form(x.name)
  3. next
This is a minor difference, and I think it is unlikely that it will help, but there's always a chance. Have you seen whether you can read from the db at this point? You should be able to read/edit/modify when you open the db in this way. Try, after the "for each" line (before the rest of the loop is executed)
Expand|Select|Wrap|Line Numbers
  1. response.write "updating "& x.name & "<br>" & vbNewLine
This should just print out "updating name" or whatever field names you have in your db.

Jared
Aug 10 '07 #10
nkoriginal
23 New Member
if that doesn't work, you could also try:
Expand|Select|Wrap|Line Numbers
  1. for each x in objRS.fields
  2.    objRS(x.name) = request.form(x.name)
  3. next
This is a minor difference, and I think it is unlikely that it will help, but there's always a chance. Have you seen whether you can read from the db at this point? You should be able to read/edit/modify when you open the db in this way. Try, after the "for each" line (before the rest of the loop is executed)
Expand|Select|Wrap|Line Numbers
  1. response.write "updating "& x.name & "<br>" & vbNewLine
This should just print out "updating name" or whatever field names you have in your db.

Jared
Jared, thank you for your help, I tried all you said but it didn't work.
I've the same error in the same line.


objRS(x) = request.form(x) or
objRS(x.name) = request.form(x.name)

I checked my database permission and everything is fine. It's not read-only.
I've many form in differents way, update, delete, insert and never has a similiar problem like this one.
I dont know what I can do..

Do u have another idea for help in this problem?
Maybe create an store procedure, but I dont know how to do it.

thank you
NIKO
Aug 13 '07 #11
jhardman
3,406 Recognized Expert Specialist
Try, after the "for each" line (before the rest of the loop is executed)
Expand|Select|Wrap|Line Numbers
  1. response.write "updating "& x.name & "<br>" & vbNewLine
This should just print out "updating name" or whatever field names you have in your db.
what about the second suggestion. Did you try that? This gives some indication of what the problem is, if this line doesn't give an error, than the db is reading just fine. If it loops through several time before you get an error, you know that only one of the fields is having problems. If it never works, then there is some problem connecting to the db at all.

Jared
Aug 13 '07 #12
nkoriginal
23 New Member
what about the second suggestion. Did you try that? This gives some indication of what the problem is, if this line doesn't give an error, than the db is reading just fine. If it loops through several time before you get an error, you know that only one of the fields is having problems. If it never works, then there is some problem connecting to the db at all.

Jared
Jared:

Yes I tried with the 2 alternatives.
When I put the response.write "updating& x.name &" .....

I received this message in the browser

updating id

Microsoft OLE DB Provider for SQL Server error '80004005'

Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.

/mackoul/beta/forms/borrar.asp, line 30


Now, this my complete code for a simple form with 2 fields
Expand|Select|Wrap|Line Numbers
  1. <!--#include virtual="/mackoul/beta/includes/connection.asp"-->
  2.  
  3. <% if request.Form("submit") = "submit" then%>
  4.  
  5.  
  6. <%
  7.  
  8.             Set sshf = Server.CreateObject("ADODB.Recordset")
  9.             sshf.ActiveConnection = MM_mackoul_STRING
  10.             sshf.CursorType = 0
  11.             sshf.CursorLocation = 2
  12.             sshf.LockType = 3
  13.             sshf.source = "select * from t1"
  14.             'response.Write (sshf.source)            
  15.             sshf.open
  16.  
  17.  
  18.  
  19.  
  20. '     objRS.addNew
  21. '     for each x in objRS.fields
  22. '      response.write "updating "& x.name & "<br>" & vbNewLine
  23. '           objRS(x.name) = request.form(x.name)
  24. '      next
  25.      sshf.addNew
  26.      for each x in sshf.fields
  27.       response.write "updating "& x.name & "<br>" & vbNewLine
  28.          sshf(x.name) = request.form(x)
  29.       next
  30.  
  31.       sshf.update
  32. %>
  33.  
  34.  
  35. <% else %>
  36.  
  37. Press submit!
  38.  
  39. <form action="borrar.asp" method="post" name="form">
  40. name1<input name="name1" id="name1" type="text" size="10" maxlength="10"><bR>
  41. name2<input name="name2" id="name2" type="text" size="10" maxlength="10"><bR>
  42. <input name="submit" type="submit" value="submit">
  43. </form>
  44. <% end if %>
  45.  
I've another form with 100 fields and I need this procedure for that form. I working with this one, because I think is more easy to work with 2 field than 100 hehe.

Thank you
Aug 13 '07 #13
jhardman
3,406 Recognized Expert Specialist
I'm just about out of ideas. This shows that the connection is definitely active. It displays one of the recordset's field names. I don't know. did you try cursortype=2?

Jared
Aug 13 '07 #14
nkoriginal
23 New Member
I'm just about out of ideas. This shows that the connection is definitely active. It displays one of the recordset's field names. I don't know. did you try cursortype=2?

Jared
Yes I tried with Cursotype = 2 But nothing.
I will try another way if I found something I will post a new message.

thank you Jared
NIKO
Aug 13 '07 #15
nkoriginal
23 New Member
Yes I tried with Cursotype = 2 But nothing.
I will try another way if I found something I will post a new message.

thank you Jared
NIKO
Jared:

I found the solution to my problem.
Thank you for your help.

Here is the complete code for insert X fields from HTML form in SQL table.


Dim rs, data_source
Set rs = Server.CreateObject("ADODB.Recordset")

rs.ActiveConnection = MM_mm_STRING
rs.CursorType = 2
rs.CursorLocation = 2
rs.LockType = 3
rs.source = "select * from t1"
response.Write (rs.source)
rs.open

rs.addNew
for each X in request.Form
if ucase(X) <> "SUBMIT" AND ucase(X) <> "UNIQUEID" and left(ucase(X),4) <> "DROP" and ucase(X) <> "OVERRIDE" then

Response.Write "<BR>"
Response.Write X & " = " & Request.Form( X ) & "<BR>"
rs(x) = request.form(x)
end if
next
rs.update

rs.close
set rs = nothing

If somebody has any problem with this code, please let me know

Thank you again Jared

NIKO
Aug 13 '07 #16
jhardman
3,406 Recognized Expert Specialist
Doh! Was the entire problem that the script was trying to update an autonumber? I should have seen that. I think I've even answered that question in the past. Anyway, I'm glad you found the sol'n.

Jared
Aug 13 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alessandro GARDICH | last post by:
Hi to all ... I'm looking to a way to auto update some fields of a row when other fileds are updated. the table structure are simple, CREATE TABLE param ( id int4 PRIMARY KEY, val int4,
4
by: m_houllier | last post by:
STUDENT TABLE StudentReference Student Name etc ATTENDANCE TABLE AttendanceID CourseID StudentReference
1
by: Jim | last post by:
I have an asp.net site and I want to provide automatic login when a user returns to my site, similar to amazon.com. Currently my site authenticates the user and generates a token when the user...
2
by: anony | last post by:
Maybe this feature is already out there. I guess you could write triggers to do some of this. Often when designing a database I add a start_date and end_date column to the table. The start_date...
1
by: klimmass | last post by:
Hello Does anybody know a tool/macro with can automatically add a header comment to a .cs file at saving it ? I want something like this: eg. ///Automatic comment //CREATED BY: klimmass...
2
by: hmario | last post by:
Hi, it's possibile to define table fields for automatic Last_Update_Date and for Creation_date using column formula?? Thanks !!!
3
by: myjish18 | last post by:
Hello, We have a DB2 UDB database v8.2.7 (db2 v8.2 fixpak 14) on AIX 5.3 which has Automatic Storage (AS) enabled. We want to disable automatic storage on entire database and/or disable...
25
by: azura | last post by:
<input name="matric" type="text" class="style26" id="matric" value="<?php echo $row_rs_mas; ?>" size="15" /> <------ this input that i insert those matric_no <input name="name" type="text"...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.