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="thisSamePage.asp">
<input type="text" name="userName">User Name<br>
<input type="text" name="phonNumber">Phone Number<br>
<input type="text" name="toothBrushColor">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: - dim x
-
for each x in request.form
-
response.write x & ": " & request(x) & "<br>" & vbNewLine
-
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: - dim objConn, objRS
-
set objConn = server.createObject("adodb.connection")
-
objConn.Provider="Microsoft.Jet.OLEDB.4.0"
-
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: - dim query
-
set objRS = server.creatObject("adodb.recordset")
-
query = "SELECT * FROM userTable"
-
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: - objRS.addNew
-
objRS("userName") = request("userName")
-
objRS("phonNumber") = request("phonNumber")
-
objRS("toothBrushColor") = request("toothBrushColor")
-
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: - dim x
-
objRS.addNew
-
for each x in objRS.fields
-
if request.form(x) <> "" then
-
objRS(x) = request.form(x)
-
end if
-
next
-
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.
10 28795
how to connect if you're using c#
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
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(dreamweraver)
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_done.asp -
<%
-
codeno=Request("code_no")
-
partno=Request("prt_no")
-
product=Request("prd")
-
brand=Request("brand")
-
auto=Request("auto")
-
unitprice=Request("unit_price")
-
size=Request("size")
-
height=Request("height")
-
outd1=Request("out_d1")
-
outd2=Request("out_d2")
-
ind1=Request("in_d1")
-
ind2=Request("in_d2")
-
trepno=Request("t_rep")
-
rpno1=Request("rp_no1")
-
rpno2=Request("rp_no2")
-
rpno3=Request("rp_no3")
-
rpno4=Request("rp_no4")
-
rpno5=Request("rp_no5")
-
rpno6=Request("rp_no6")
-
-
-
Dim Connstr, ADSTK, strSQL
-
set ADSTK=Server.CreateObject("ADODB.Recordset")
-
set
-
Connstr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db/db1.mdb")
-
//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;"
-
strSQL = "SELECT * FROM Stk_items;"
-
-
ADSTK.CursorType = 2
-
ADSTK.LockType = 3
-
ADSTK.Open strSQL, Connstr, 2
-
ADSTK.AddNew
-
ADSTK.Fields("Code_no")=codeno
-
ADSTK.Fields("Prt_no")=partno
-
ADSTK.Fields("Prd")=product
-
ADSTK.Fields("Brand")=brand
-
ADSTK.Fields("Auto")=auto
-
ADSTK.Fields("U_price")=unitprice
-
ADSTK.Fields("Size")=size
-
ADSTK.Fields("Height")=height
-
ADSTK.Fields("O_d1")=outd1
-
ADSTK.Fields("O_d2")=outd2
-
ADSTK.Fields("I_d1")=ind1
-
ADSTK.Fields("I_d2")=ind2
-
ADSTK.Fields("T_rep_no")=trepno
-
ADSTK.Fields("Rep1")=rpno1
-
ADSTK.Fields("Rep2")=rpno2
-
ADSTK.Fields("Rep3")=rpno3
-
ADSTK.Fields("Rep4")=rpno4
-
ADSTK.Fields("Rep5")=rpno5
-
ADSTK.Fields("Rep6")=rpno6
-
ADSTK.Update
-
ADSTK.Close
-
set ADSTK = Nothing
-
set Connstr = Nothing
-
Response.Redirect("Ad_stk.asp")
-
%>
-
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(dreamweraver)
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
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... -
function Addstk()
-
{
-
var flg;
-
var str;
-
str += "Please Fill in the following";
-
str += "\n**************";
-
-
if((document.forms[0].code_no.value)=="")
-
{
-
str += "\n Enter Code No";
-
flg = 0;
-
}
-
if((document.forms[0].prt_no.value)=="")
-
{
-
str += "\n Enter Part No";
-
flg = 0;
-
}
-
if((document.forms[0].prd.value)=="none")
-
{
-
str += "\n Enter Product";
-
flg = 0;
-
}
-
if((document.forms[0].brand.value)=="none")
-
{
-
str += "\n Enter Brand";
-
flg = 0;
-
}
-
if((document.forms[0].auto.value)=="")
-
{
-
str += "\n Enter AutoMobile";
-
flg = 0;
-
}
-
if((document.forms[0].unit_price.value)=="")
-
{
-
str += "\n Enter Unit Price";
-
flg = 0;
-
}
-
if((document.forms[0].qtty.value)=="")
-
{
-
str += "\n Enter Quantity";
-
flg = 0;
-
}
-
if((document.forms[0].t_rep.value)=="none")
-
{
-
str += "\n Enter Total Replace No";
-
flg = 0;
-
}
-
if((document.forms[0].size.value)=="none")
-
{
-
str += "\n Enter Size";
-
flg = 0;
-
}
-
if((document.forms[0].height.value)=="")
-
{
-
str += "\n Enter Height";
-
flg = 0;
-
}
-
if((document.forms[0].out_d1.value)=="")
-
{
-
str += "\n Enter Outer Dia1";
-
flg = 0;
-
}
-
if((document.forms[0].out_d2.value)=="")
-
{
-
str += "\n Enter Outer Dia2";
-
flg = 0;
-
}
-
if((document.forms[0].in_d1.value)=="")
-
{
-
str += "\n Enter Inner Dia1";
-
flg = 0;
-
}
-
if((document.forms[0].in_d2.value)=="")
-
{
-
str += "\n Enter Inner Dia2";
-
flg = 0;
-
}
-
if(flg == 0)
-
{
-
alert(str);
-
return false;
-
}
-
else
-
{
-
ducument.forms[0].action="adstk_done.asp";
-
document.forms[0].submit();
-
return true;
-
}
-
-
}
-
and the form tag was like this -
<HTML>
-
<form name="addstkfrm" method="post">
-
</HTML>.
-
note the last very last else
And what i did was i removed these 2 lines -
document.forms[0].action()="adstk_done.asp";
-
document.forms[0].submit();
-
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 -
<form name="adstkfrm" method="post" action="adstk_done.asp">
-
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
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.
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.
one should specify the lock type as default lock type doesnt support inserting data into database using record set object
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
I know that this is an old article and that ASP is not quite so popular these days, but anywho:
One can vastly simplify the amount of database connection and recordset code that clutters up ASP pages by creating a library of Subs and calling them in place:
Consider: -
OpenDB conn, site("DBQ"), rs, "SELECT stuff FROM table" ' Site("DBQ") contains the connection string.
-
Versus: -
Dim conn
-
Dim rs
-
set conn = server.createObject("adodb.connection")
-
conn.Provider="Microsoft.Jet.OLEDB.4.0"
-
conn.Open "c:/somedatabase.mdb"
-
-
dim strSQL
-
set rs = server.creatObject("adodb.recordset")
-
strSQL= "SELECT * FROM userTable"
-
rs.open strSQL, conn
-
The OpenDB Sub itself makes calls to smaller subs with ByRef and ByVal arguments which do all of the work, so if you wanted to open a secondary recordset, for example to fill select box options, you would call the relevant Sub directly: -
OpenRS rs2, conn, "SELECT some more stuff FROM adifferenttable"
-
The closing statements are just as simple: -
CloseRS rs2 ' this closes the rs and sets it to nothing.
-
CloseDB conn, rs ' as above but for both the conn object and the recordset.
-
Gaz
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |