Thursday, July 9, 2009

How to add new record in database and save the database using ASP?

Hello I am beginner of ASP coding. I can carry data from database using 'select' statement. But how can I insert the new record and update database? I am using MS Access. I am inserting value of the fields from HTML file's text boxes. I need code or any tutorial for free from any other site. Thanks....

How to add new record in database and save the database using ASP?
You form fields need to be requested. Once you have them, you can mnake them a part of your INSERT statement. Here is working code for the form and insert page. As long as you are connectinog to the database.


Form Code





%26lt;form action="CustomerInsert.asp" method="POST" name="Customer_Form" %26gt;


%26lt;table border='1' cellpadding='3' cellspacing='0'%26gt;





%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;Fname: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='Fname' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;Lname: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='Lname' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;Address 1: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='Address1' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;Address 2: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='Address2' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;City: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='City' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;State: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='State' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td%26gt;%26lt;font face='Arial' size='2'%26gt;Zip: %26lt;/font%26gt;%26lt;/td%26gt;%26lt;td%26gt;%26lt;input type='text' name='Zip' size="40"%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;


%26lt;tr%26gt;%26lt;td colspan='2' align='center'%26gt;%26lt;input type='submit' value='Submit' name='Submit' style='font-family: Arial; font-size: 10pt'%26gt; %26lt;input type='reset' value='Reset' name='B2' style='font-family: Arial; font-size: 10pt'%26gt;%26lt;/td%26gt;%26lt;/tr%26gt;





%26lt;/table%26gt;





%26lt;/form%26gt;








Insert Page Code





%26lt;%





'*** Variables ****


Dim DBConn


Dim strSQL


Dim RS


Dim Fname


Dim Lname


Dim Address1


Dim Address2


Dim City


Dim State


Dim Zip








'*** Form Requests ****


Fname = Request.Form("Fname")


Lname = Request.Form("Lname")


Address1 = Request.Form("Address1")


Address2 = Request.Form("Address2")


City = Request.Form("City")


State = Request.Form("State")


Zip = Request.Form("Zip")








'*** Replace Single Quotes (') ****


Fname = Replace(Fname,"'","''")


Lname = Replace(Lname,"'","''")


Address1 = Replace(Address1,"'","''")


Address2 = Replace(Address2,"'","''")


City = Replace(City,"'","''")


State = Replace(State,"'","''")


Zip = Replace(Zip,"'","''")








'*** SQL Statement ****


strSQL = "INSERT INTO Customers (Fname, Lname, Address1, Address2, City, State, Zip) VALUES ('" %26amp; Fname %26amp; "', '" %26amp; Lname %26amp; "', '" %26amp; Address1 %26amp; "', '" %26amp; Address2 %26amp; "', '" %26amp; City %26amp; "', '" %26amp; State %26amp; "', '" %26amp; Zip %26amp; "')"





'*** DB Include Connection ***


%%26gt;


%26lt;!-- #Include File="DBInc.asp"--%26gt;


%26lt;%





DBConn.Execute (strSQL)





DBConn.Close


Set DBConn = Nothing





Response.Redirect "Default.asp"





%%26gt;
Reply:Let me know if you need more help. Report It

Reply:There is a great free tutorial here. Check it out:
Reply:http://www.asp101.com/samples/db_add.asp


No comments:

Post a Comment