We help IT Professionals succeed at work.

How do I send parameter to Store procedure?

erin027
erin027 asked
on
410 Views
Last Modified: 2012-05-05
Hello,
I need help Experts!
I am a beginner of MS-SQL and ASP.
I made a store prPcedure on MS-SQL (See below) and I need to send 2 parameters to SQL and I have no idea how to do it. I have searched internet and this is my coding so far...

SQL Store procedure:

-- Create Table
create table test (
      firstname nvarchar(10)
,      lastname nvarchar(10)
)

-- Create Store Procedure
CREATE PROCEDURE CreateNewMember2  (@firstname nvarchar(10) , @lastname nvarchar(10))
AS
SET NOCOUNT ON

INSERT INTO test (firstname,lastname)
VALUES (@firstname, @lastname)

GO


ASP CODING:
<!--#include file ="../function/dbopen.asp"-->

<%
-- I brought two values from previous page which I wanted to input on my data with store procedure
firstname2 = request("firstname")
lastname2= request("lastname")

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
    .ActiveConnection = db
    .Commandtext = "CreateNewMember2 firstname, lastname"
    .CommandType = adCmdStoredProc
      .Parameters.Append .CreateParameter("@firstname", advarchar,adParamInput,10)
      .Parameters.Append .CreateParameter("@lastname",adnvarWchar,adParamInput,10)
    .Parameters("@firstname") = firstname2
      .Parameters("@lastname") = lastname2
    .Execute , , adExecuteNoRecords

End with

Set cmd = Nothing

db.close
Set db = Nothing

%>


I am not familiar with Parameter and command object. I copied from other forum so if you can explain about on how to use command and parameter, it will be a big help!
Thank you guys!
Comment
Watch Question

Database Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Jai STech Arch

Commented:
another way is to directly call the sp
  .Commandtext = "CreateNewMember2 '" & firstname  &"','" & lastname & "'"
    .CommandType = adCmdStoredProc
'      .Parameters.Append .CreateParameter("@firstname", advarchar,adParamInput,10)
'      .Parameters.Append .CreateParameter("@lastname",adnvarWchar,adParamInput,10)
'    .Parameters("@firstname") = firstname
'      .Parameters("@lastname") = lastname2
    .Execute , , adExecuteNoRecords

and if you want to pass parameteres only then
Commandtext = "CreateNewMember2 @firstname, @lastname"


Author

Commented:
Thanks for the Comment.
However, I changed the coding and I am still getting an error message

Changed Code:
<!--#include file ="../function/dbopen.asp"-->

<%
firstname2 = request("firstname")
lastname2 = request("lastname")

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
    .ActiveConnection = db
    .Commandtext = "CreateNewMember2 firstname, lastname"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter("@firstname", advarchar,adParamInput,10)
    .Parameters.Append .CreateParameter("@lastname", advarchar,adParamInput,10)
    .Parameters("@firstname").Value = firstname2
    .Parameters("@lastname").Value = lastname2
    .Execute , , adExecuteNoRecords

End with

Set cmd = Nothing

db.close
Set db = Nothing
%>

And I am getting a following error message:
Microsoft OLE DB Provider for SQL Server error '80040e14'
Syntax error or access violation
/member/test.asp, line 19

The line 19 was >>>>   .Execute , , adExecuteNoRecords

Also, am I doing in a right way? Is there any other way to do this?
I am new with everything so any advise will help me.
Thank you so much

Author

Commented:
Ignore my previous Message.
It worked. Thank you!!!!!!!!!!!!!!!!!!!!!!!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.