473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert data into a database with ASP

jhardman
3,406 Recognized Expert Specialist
step 1- get data.

for example, make a form with one input field for each piece of data you want to put in the db:[html]<form method="post" action="thisSam ePage.asp">
<input type="text" name="userName" >User Name<br>
<input type="text" name="phonNumbe r">Phone Number<br>
<input type="text" name="toothBrus hColor">Color of your Toothbrush<br>
<input type="submit" value="submit"> </form>[/html]
For convenience, it helps to make the name of the input identical to the name of the fields of the database.

It is a good idea to make sure you got data at this point, so list it like this:
Expand|Select|Wrap|Line Numbers
  1. dim x
  2. for each x in request.form
  3.    response.write x & ": " & request(x) & "<br>" & vbNewLine
  4. next
you got data? good.

step 2- connect to the database.

Unfortunately, ability to connect to a db is not built directly into aspwhen it was originally made, but it was added later in the form of a series of server objects. It is a bit awkward and that is just the way it goes. The connection looks like this:
Expand|Select|Wrap|Line Numbers
  1. dim objConn, objRS
  2. set objConn = server.createObject("adodb.connection")
  3. objConn.Provider="Microsoft.Jet.OLEDB.4.0"
  4. objConn.Open "c:/inetpub/myDBFiles/myDBofUsers.mdb"
step 3- There are several ways to work with the db once you are connected. I prefer to do it by creating a recordset like this:
Expand|Select|Wrap|Line Numbers
  1. dim query
  2. set objRS = server.creatObject("adodb.recordset")
  3. query = "SELECT * FROM userTable"
  4. objRS.open query, objConn
This gives you an object named "objRS" which is a little copy of one table of the db.

step 4- To add a new record, do this:
Expand|Select|Wrap|Line Numbers
  1. objRS.addNew
  2. objRS("userName") = request("userName")
  3. objRS("phonNumber") = request("phonNumber")
  4. objRS("toothBrushColor") = request("toothBrushColor")
  5. objRS.update
Now you have added data to a db. That wasn't so hard, was it?

Jared

PS Remember how I said it was convenient to use the exact db field names as the name of your form inputs? If you did this, then step four can be simplified to this:
Expand|Select|Wrap|Line Numbers
  1. dim x
  2. objRS.addNew
  3. for each x in objRS.fields
  4.    if request.form(x) <> "" then
  5.       objRS(x) = request.form(x)
  6.    end if
  7. next
  8. objRS.update
This opens the db and goes through each field and looks for a form input with the same name. If it finds one, that input is used to populate the corresponding db field.
Jul 5 '07 #1
10 28839
gp2060
1 New Member
how to connect if you're using c#
Nov 17 '07 #2
jhardman
3,406 Recognized Expert Specialist
how to connect if you're using c#
You can't write ASP pages in c#, so your question is meaningless (or it would be meaningless if you had written a question). Try the .NET forum.

Jared
Nov 17 '07 #3
omerbutt
638 Contributor
as i was looking through the questions and articles i came by ur article sir iam also using the same thing or say the same way to insert in to a data base but if i use the objrs.AddNew after i open query and conn it adds the record twice means each input in the form that i submit is entered twice .....and if i dont use the objrs.AddNew then it edits the very first record in the data base means it overwrites the 1st record in the table :( y is that so for ur convienience i am writing the code here, and yes one thing also if i exit the editor(dreamwer aver)
and explorer windows OR if i edit the code and then save it without any query that gives error and then again write the same query then the first time it enters only once but as i continue to enter the data through the form it tarts adding the record twice again can u help in the matter

file name:Addstk_don e.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2.                codeno=Request("code_no")
  3.     partno=Request("prt_no")
  4.     product=Request("prd")
  5.     brand=Request("brand")
  6.     auto=Request("auto")
  7.     unitprice=Request("unit_price")
  8.     size=Request("size")
  9.     height=Request("height")
  10.     outd1=Request("out_d1")
  11.     outd2=Request("out_d2")
  12.     ind1=Request("in_d1")
  13.     ind2=Request("in_d2")
  14.     trepno=Request("t_rep")
  15.     rpno1=Request("rp_no1")
  16.     rpno2=Request("rp_no2")
  17.     rpno3=Request("rp_no3")
  18.     rpno4=Request("rp_no4")
  19.     rpno5=Request("rp_no5")
  20.     rpno6=Request("rp_no6")
  21.  
  22.  
  23.     Dim Connstr, ADSTK, strSQL
  24.     set ADSTK=Server.CreateObject("ADODB.Recordset")    
  25.     set
  26.     Connstr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/db1.mdb")
  27.     //strSQL = "Select Stk_items.Code_no, Stk_items.Prt_no, Stk_items.Prd, Stk_items.Brand, Stk_items.Auto, Stk_items.U_price, Stk_items.Size, Stk_items.Height, Stk_items.O_d1, Stk_items.O_d2, Stk_items.I_d1, Stk_items.I_d2, Stk_items.T_rep_no, Stk_items.Rep1, Stk_items.Rep2, Stk_items.Rep3, Stk_items.Rep4, Stk_items.Rep5, Stk_items.Rep6 From Stk_items;"
  28.     strSQL = "SELECT * FROM Stk_items;"
  29.  
  30.     ADSTK.CursorType = 2
  31.     ADSTK.LockType = 3    
  32.     ADSTK.Open strSQL, Connstr, 2
  33.     ADSTK.AddNew    
  34.     ADSTK.Fields("Code_no")=codeno
  35.     ADSTK.Fields("Prt_no")=partno
  36.     ADSTK.Fields("Prd")=product
  37.     ADSTK.Fields("Brand")=brand
  38.     ADSTK.Fields("Auto")=auto
  39.     ADSTK.Fields("U_price")=unitprice
  40.     ADSTK.Fields("Size")=size
  41.     ADSTK.Fields("Height")=height
  42.     ADSTK.Fields("O_d1")=outd1
  43.     ADSTK.Fields("O_d2")=outd2
  44.     ADSTK.Fields("I_d1")=ind1
  45.     ADSTK.Fields("I_d2")=ind2
  46.     ADSTK.Fields("T_rep_no")=trepno
  47.     ADSTK.Fields("Rep1")=rpno1
  48.     ADSTK.Fields("Rep2")=rpno2
  49.     ADSTK.Fields("Rep3")=rpno3
  50.     ADSTK.Fields("Rep4")=rpno4
  51.     ADSTK.Fields("Rep5")=rpno5
  52.     ADSTK.Fields("Rep6")=rpno6    
  53.     ADSTK.Update
  54.     ADSTK.Close
  55.     set ADSTK = Nothing
  56.     set Connstr = Nothing
  57.     Response.Redirect("Ad_stk.asp")
  58. %>
  59.  
Nov 25 '07 #4
jhardman
3,406 Recognized Expert Specialist
as i was looking through the questions and articles i came by ur article sir iam also using the same thing or say the same way to insert in to a data base but if i use the objrs.AddNew after i open query and conn it adds the record twice means each input in the form that i submit is entered twice .....and if i dont use the objrs.AddNew then it edits the very first record in the data base means it overwrites the 1st record in the table :( y is that so for ur convienience i am writing the code here, and yes one thing also if i exit the editor(dreamwer aver)
and explorer windows OR if i edit the code and then save it without any query that gives error and then again write the same query then the first time it enters only once but as i continue to enter the data through the form it tarts adding the record twice again can u help in the matter
I don't see anything in this code to suggest why that might happen. Are you doing anything to monitor or kill double-posts?

Jared
Nov 27 '07 #5
omerbutt
638 Contributor
no nothing like that is happening ......... :) ...but infact yesterday i found the mistake that fixed the problem the mistake don looks like a mistake to me ......well u r gonna decide .....
the form through which i was submitting my data used a button that called a javascript function in which i validate
Addstk()---->that stands for add stock
in the funtion i am applying constraints and check for empty input lemme show u the code that i was using before then i wil tell u a lil change that i made or was that a mistake...
Expand|Select|Wrap|Line Numbers
  1. function Addstk()
  2. {
  3.                 var flg;        
  4.     var str;
  5.     str += "Please Fill in the following";
  6.     str += "\n**************";
  7.  
  8.     if((document.forms[0].code_no.value)=="")
  9.     {
  10.         str += "\n Enter Code No";
  11.         flg = 0;            
  12.     }
  13.     if((document.forms[0].prt_no.value)=="")
  14.     {
  15.         str += "\n Enter Part No";
  16.         flg = 0;
  17.     }
  18.     if((document.forms[0].prd.value)=="none")
  19.     {
  20.         str += "\n Enter Product";
  21.         flg = 0;
  22.     }
  23.     if((document.forms[0].brand.value)=="none")
  24.     {
  25.         str += "\n Enter Brand";
  26.         flg = 0;
  27.     }
  28.     if((document.forms[0].auto.value)=="")
  29.     {
  30.         str += "\n Enter AutoMobile";
  31.         flg = 0;
  32.     }
  33.     if((document.forms[0].unit_price.value)=="")
  34.     {
  35.         str += "\n Enter Unit Price";
  36.         flg = 0;
  37.     }
  38.     if((document.forms[0].qtty.value)=="")
  39.     {
  40.         str += "\n Enter Quantity";
  41.         flg = 0;
  42.     }
  43.     if((document.forms[0].t_rep.value)=="none")
  44.     {
  45.         str += "\n Enter Total Replace No";
  46.         flg = 0;
  47.     }
  48.     if((document.forms[0].size.value)=="none")
  49.     {
  50.         str += "\n Enter Size";
  51.         flg = 0;
  52.     }
  53.     if((document.forms[0].height.value)=="")
  54.     {
  55.         str += "\n Enter Height";
  56.         flg = 0;
  57.     }
  58.     if((document.forms[0].out_d1.value)=="")
  59.     {
  60.         str += "\n Enter Outer Dia1";
  61.         flg = 0;
  62.     }
  63.     if((document.forms[0].out_d2.value)=="")
  64.     {
  65.         str += "\n Enter Outer Dia2";
  66.         flg = 0;
  67.     }
  68.     if((document.forms[0].in_d1.value)=="")
  69.     {
  70.         str += "\n Enter Inner Dia1";
  71.         flg = 0;
  72.     }
  73.     if((document.forms[0].in_d2.value)=="")
  74.     {
  75.         str += "\n Enter Inner Dia2";
  76.         flg = 0;
  77.     }
  78.     if(flg == 0)
  79.     {
  80.         alert(str);
  81.         return false;
  82.     }
  83.     else
  84.     {
  85.                     ducument.forms[0].action="adstk_done.asp";
  86.                                 document.forms[0].submit();
  87.                      return true;
  88.     }
  89.  
  90. }
  91.  
and the form tag was like this

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <form name="addstkfrm"  method="post">
  3. </HTML>.
  4.  
note the last very last else

And what i did was i removed these 2 lines
Expand|Select|Wrap|Line Numbers
  1. document.forms[0].action()="adstk_done.asp";
  2. document.forms[0].submit();
  3.  
and added the action property in the form tag and the specified the same file that i was using to submit in the js function given above


Expand|Select|Wrap|Line Numbers
  1. <form name="adstkfrm" method="post" action="adstk_done.asp">
  2.  
wasnt that stupid :D i don know weather its a logical mistake by me or abnormal behaviour of the application u temme and with this i have made more than 50 entries and none them were duplicated
tanks,
regards Omer



I don't see anything in this code to suggest why that might happen. Are you doing anything to monitor or kill double-posts?

Jared
Nov 27 '07 #6
jhardman
3,406 Recognized Expert Specialist
no nothing like that is happening ......... :) ...but infact yesterday i found the mistake that fixed the problem the mistake don looks like a mistake to me ......well u r gonna decide .....
the form through which i was submitting my data used a button that called a javascript function in which i validate
Addstk()---->that stands for add stock
in the funtion i am applying constraints and check for empty input lemme show u the code that i was using before then i wil tell u a lil change that i made or was that a mistake...

and the form tag was like this


note the last very last else

And what i did was i removed these 2 lines
and added the action property in the form tag and the specified the same file that i was using to submit in the js function given above



wasnt that stupid :D i don know weather its a logical mistake by me or abnormal behaviour of the application u temme and with this i have made more than 50 entries and none them were duplicated
tanks,
regards Omer
Yes, I'm not a javascript expert, but it appears the mistake was in the line which says to re-submit the form, it was already submitted, you don't have to submit it again, I think the way you handled it is preferred. The form is supposed to have all those attributes. If you decide to set those attributes with javascript you should have a very good reason for doing so.

Jared

PS, Please stop writing in slang. This is an international site and it is very difficult for many readers to understand when you use "u temme" or "u r gonna" or similar incorrect English. I personally find it very annoying. I had to stop myself from insulting you just now.
Nov 28 '07 #7
omerbutt
638 Contributor
yeah obviously you are right
and yes sori about that slang you know while writing it looks a little quick and easy thats why ....anyways i wud watch that next time
regards ,
Omer
Yes, I'm not a javascript expert, but it appears the mistake was in the line which says to re-submit the form, it was already submitted, you don't have to submit it again, I think the way you handled it is preferred. The form is supposed to have all those attributes. If you decide to set those attributes with javascript you should have a very good reason for doing so.

Jared

PS, Please stop writing in slang. This is an international site and it is very difficult for many readers to understand when you use "u temme" or "u r gonna" or similar incorrect English. I personally find it very annoying. I had to stop myself from insulting you just now.
Nov 29 '07 #8
chirag1989
28 New Member
one should specify the lock type as default lock type doesnt support inserting data into database using record set object
Jan 20 '08 #9
jhardman
3,406 Recognized Expert Specialist
one should specify the lock type as default lock type doesnt support inserting data into database using record set object
Thanks, I didn't notice that I had left that out.

Jared
Jan 20 '08 #10

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

Similar topics

2
2854
by: Santo Santis | last post by:
How can I make an .exe file that can insert data automatically in a SQLServer database, I can do it in C, but how can I connect to SQLServer and execute a query. All data that I have to insert are data that I can have from PC environment variables. Thanks
0
3981
by: Dinu | last post by:
Hi All I am trying to insert data within a transaction, into a Db2 database from asp.net. I have created a System DSN using ISeries Access Driver(32 bit) for Windows. I am then connecting to it using Microsoft .Net data provider from asp.net. I am able to insert data by using connection and comand object without
1
2172
by: Pratik Gupte | last post by:
I have created a database in .mdf format, but I am unable to insert data into its tables. Can anybody help how to insert data using ASP.Net 2.0 using SQL Server 2005 Express edition in windows integrated security mode. thx
6
2105
by: VitaminB | last post by:
Hi Guys, I try to insert data from a form into a mysql database, but it did not work... There is no error, but the data did not pass thru the table. This is the short script:
3
4735
by: andrewtayloruk | last post by:
I'm a newbie when it come to things php and i'm having a bit of trouble. I'm trying to insert data from an html form into a mysql database and can't get it to work. Just a few bits about my setup, i'm running an sql server locally, i've created the database, table and fields. I think what i'm missing is something that actually runs the sql...
0
2189
by: danishce | last post by:
Hello, I want to insert data directly into my windows form data grid and load a combobox(userid) in the 1st column of data grid,a textbox(password) in 2nd column of datagrid. The code for insert data is: dim cmd as new oledbcommand("insert into table(userid,password) values('" & cmbcol1.text & "','" & txtcol2.text & "')",connection) dim da as...
1
2167
by: Doc11 | last post by:
I'm trying to allow users insert data into a database using the form view. But when I click the insert button I get this error: Server Error in '/Customer Database' Application. -------------------------------------------------------------------------------- Incorrect syntax near 'nvarchar'. Description: An unhandled exception occurred...
2
3718
by: Newbie19 | last post by:
I am creating a project that would insert data into a SQL database when the User placed the information into the VB application. Then at a click of a button, it would add this data into the database. I am wondering what is the exact or direction of the VB code to insert the data into the database. I have the connectionstring created and I...
8
6605
by: Betikci Boris | last post by:
Can not insert data into SQLite3 database through browser however i can easily insert data into my db from konsole, in both attmpts i used php 5.2.6 on 2.6.25.* linux kernel i think there is a problem with my php setup but i could not find it... here is the code;...
0
2721
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume Adapter Service Add-in create schemas to insert data into oracle database and tow-way send port binding file Then create orchestration and logical...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7962
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7814
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1949
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 we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.