Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using ASP & SQL Stored Procedures to Insert new Records

Status
Not open for further replies.

Rexolio

Technical User
Aug 29, 2001
230
I have 2 books...SQL for Dummies and Active Server Pages Bible..so far both are great. However, I have a problem with inserting new records from my web site via a registration form to a table in my SQL database. I use a similar ADO insert script for another part of my site, and it inserts very quickly. But this one has a problem and seems to take between 10 and 30 seconds to insert. I've tried everything and used this site to try and figure out what it is. I can't isolate it to any one thing.

My ASP Bible says to create a stored procedure and then use it from my web site to utilize it. SQL for Dummies doesn't tell me anything and the ASP Bible book says to refer to SQL documentation.

My question: can someone give me an example of a script, include connection and close, that will insert from a web site form to a SQL database via a stored procedure? I have the stored procedure created already. I've tried creating a script...but it keeps saying that I'm not using the right parameters (@FirstName_1)...the actual form field name and table field name is simply FirstName. The name @FirstName_1 was the name given my the Create Stored Procedure Wizard.

Thanks in advance!
 
Here is my ASP page that submits to itself,l and then inserts using sproc.


<html>
<body>
<%
var tviConnStr = &quot;Driver={SQL Server}; Server=122.133.144.155; Database=<DATABASE NAME>; UID=<USER ID>; PWD=<PASSWORD>&quot;

var lastName = String(Request.Form(&quot;lastName&quot;));
var firstName = String(Request.Form(&quot;firstName&quot;));

var phone1 = String(Request.Form(&quot;phone1&quot;));
var phone2 = String(Request.Form(&quot;phone2&quot;));
var phone3 = String(Request.Form(&quot;phone3&quot;));
var phone4 = String(Request.Form(&quot;phone4&quot;));

if (phone1 != &quot;&quot;)
var phoneNumber = &quot;(&quot; + phone1 + &quot;) &quot; + phone2 + &quot;-&quot; + phone3 + &quot; ext. &quot; + phone4;
else
var phoneNumber = &quot;&quot;;
var emailAddress = String(Request.Form(&quot;emailAddress&quot;));
var webpage = String(Request.Form(&quot;webpage&quot;));
var customPage = String(Request.Form(&quot;customPage&quot;));
var location = String(Request.Form(&quot;location&quot;));
var comments = String(Request.Form(&quot;comments&quot;));
var bannerFilename = String(Request.Form(&quot;bannerFilename&quot;));
var memory = String(Request.Form(&quot;memory&quot;));

//See if memory function is set to &quot;Yes&quot;
var remember = (memory == &quot;Yes&quot;);

var updateOK;

if (location != &quot;undefined&quot;)
{
//Creates ADO Connection object and opens it
var connTviDB = Server.CreateObject(&quot;ADODB.Connection&quot;);
connTviDB.Open(tviConnStr,&quot;USERNAME&quot;,&quot;PASSWORD&quot;);

//Creates ADO Command object
var cmdInsertPhot = Server.CreateObject(&quot;ADODB.Command&quot;);

with (cmdInsertPhot)
{
ActiveConnection = connTviDB; //Active connection set to connTviDB
CommandText = &quot;uspInsertPhotographer&quot;; //Name of stored procedure
CommandType = 4;

//Appending new parameters
Parameters.Append(CreateParameter (&quot;RETURN_VALUE&quot;,3,4) );
Parameters.Append(CreateParameter (&quot;@lastName&quot;,200,1,50,lastName) );
Parameters.Append(CreateParameter (&quot;@firstName&quot;,200,1,50,firstName) );
Parameters.Append(CreateParameter (&quot;@phoneNumber&quot;,200,1,50,phoneNumber) );
Parameters.Append(CreateParameter (&quot;@location&quot;,200,1,2,location) );
Parameters.Append(CreateParameter (&quot;@bannerFilename&quot;,200,1,50,bannerFilename) );
Parameters.Append(CreateParameter (&quot;@webpage&quot;,200,1,100,webpage) );
Parameters.Append(CreateParameter (&quot;@emailAddress&quot;,200,1,100,emailAddress) );
Parameters.Append(CreateParameter (&quot;@comments&quot;,200,1,1000,comments) );
Parameters.Append(CreateParameter (&quot;@customPage&quot;,200,1,50,customPage) );

Execute();
updateOK = Parameters(&quot;RETURN_VALUE&quot;);
}

//If record was added successfully, updateOK is 1, otherwise it is set to 0
if (updateOK == 1)
{
res = &quot;<center>&quot;;
res += &quot;<img src='../images/noproblem.gif' border=0 align=absmiddle>&nbsp;&nbsp;&nbsp;&quot;;
res += &quot;<font color=#007172>&quot;;
res += &quot;Photographer <i><b><font color='#0066FF'>&quot; + firstName + &quot; &quot; + lastName + &quot;</font></i></b> has been added successfully. &quot;;
res += &quot;</font>&quot;;
}
else
{
res = &quot;<center>&quot;;
res += &quot;<img src='../images/problem.gif' border=0 align=absmiddle>&nbsp;&nbsp;&nbsp;&quot;;
res += &quot;<font color=#007172>&quot;;
res += &quot;Update failed. &quot;;
res += &quot;The casting notice <i><b><font color='#0066FF'>&quot; + cnTitle + &quot;</font></i></b> already exists in the database. &quot;;
res += &quot;</font>&quot;;
}

Response.Write(res);

} //end if (cnTitle != &quot;undefined&quot;)
%>
<form name=addForm action=addPhotographer.asp method=post>
<TABLE width=&quot;50%&quot; border=0 cellPadding=5 cellSpacing=5 align=center bgcolor=#99ccff >

<TR>
<TD>Last Name:</TD>
<TD><INPUT name=lastName></TD>
</TR>
<TR>
<TD>FirstName:</TD>
<TD><INPUT name=firstName></TD>
</TR>

<TR>
<TD>Phone Number:</TD>
<TD>
(<INPUT name=phone1 size=2 >)
<INPUT name=phone2 size=2 >
<INPUT name=phone3 size=3>&nbsp; ext. <INPUT name=phone4 size=3>
</TD>
</TR>

<TR>
<TD> Location:</TD>
<TD>
<SELECT name=location>
<OPTION selected value=&quot;&quot;>-- Select City --</OPTION>
<OPTION value=LA>Los Angeles</OPTION>
<OPTION value=&quot;NY&quot;>New York</OPTION>
</SELECT>
</TD>
</TR>

<TR>
<TD>E-mail address:</TD>
<TD><INPUT name=emailAddress ></TD>
</TR>

<TR>
<TD> Webpage:</TD>
<TD><INPUT name=webpage></TD>
</TR>

<TR>
<TD> Comments:</TD>
<TD><TEXTAREA name=comments></TEXTAREA>
</TD>
</TR>

<TR>
<TD> Banner Filename:</TD>
<TD><INPUT name=bannerFilename></TD>
</TR>

<TR>
<TD> Custom Page:</TD>
<TD><INPUT name=customPage></TD>
</TR>

<TR>
<TD>&nbsp;</TD>
<TD><INPUT type=&quot;submit&quot; value=&quot;Add&quot;>
&nbsp;
<INPUT type=checkbox value=Yes name=memory>
&nbsp;Remember Fields
</TD>
</TR>


</TABLE>
</form>
</body>
</html>


Here's my Sproc:


CREATE PROCEDURE uspInsertPhotographer

@lastName varchar(50),
@firstName varchar(50),
@phoneNumber varchar(40),
@location varchar(2),
@bannerFIlename varchar(50),
@webpage varchar(100),
@emailAddress varchar(100),
@comments varchar(1000),
@customPage varchar(50)

AS

IF NOT EXISTS (SELECT dateAdded
FROM tviPhotographers
WHERE firstName = @firstName
AND lastName = @lastName
AND location = @location
AND phoneNumber = @phoneNumber)

BEGIN

INSERT INTO tviPhotographers
(firstName, lastName, phoneNumber, location, bannerFilename, webpage, emailAddress, comments, customPage)
VALUES
(@firstName, @lastName, @phoneNumber, @location, @bannerFilename, @webpage, @emailAddress, @comments, @customPage)

RETURN 1

END

ELSE
RETURN 0




Enjoy... :)

Tell me if you got any questions.
Hope this helps. <Dmitriy>
dbrom@crosswinds.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top