Friday, March 9, 2012
Expects Parameters When It Should Not
I have a stored proc that has one output parameter. When I call
this sp from my client program (an MS Access VBA) it has always worked fine,
but just recently started to give me a message saying that the stored
procedure is expecting a parameter named @.TPRD but did not receive it,
program fails. @.TPRD is the output parameter and is declared as such. I
have not changed the sp recently so there is no code change in the sp. I
do not administrate my SQL Server, but I did request my administrator to
grant me EXEC access to sp_debug so that I could run the stored procedure
debugger. My sp stopped working at about the same time. Do you think
these two events could be related? How to solve? The code for my
stored procedure is below:
ALTER PROCEDURE [ad\jmuseck].[SELECT_TPRD]
@.TPRD VARCHAR(6) OUTPUT
AS
SELECT @.TPRD = TPRD
FROM tblCurrentMonth
RETURN @.TPRD
----
The code in my client MS Access VBA program looks like this:
Function SelectTprd(Optional newval As Variant) As String
Dim cmdCommand As ADODB.Command
Dim prmTPRD As ADODB.Parameter
Set cmdCommand = New ADODB.Command
cmdCommand.ActiveConnection = CurrentProject.AccessConnection
cmdCommand.CommandType = adCmdStoredProc
cmdCommand.CommandText = "SELECT_TPRD"
cmdCommand.Parameters.Refresh
<--STARTED FAILING ON THIS LINE !!
Set prmTPRD = cmdCommand.CreateParameter("@.TPRD", adChar, adParamOutput)
cmdCommand.Execute
'Return the time period to the calling program.'
SelectTprd = cmdCommand.Parameters(1).Value
Set cmdCommand = Nothing 'Free memory
End FunctionHello, Joe
This problem is related to MS Access, more specifically to the OLEDB
provider that Access uses internally. Use one of the following
workarounds:
1. Do not use Parameters.Refresh; instead of this, add each of the
parameters of the stored procedure (even if they have default values)
to the Parameters collection, *in the same order that they are
defined*, using something like this:
cmd.Parameters.Append cmd.CreateParameter("@.ParameterName",...)
2. Use another connection, based on the SQL Server OLEDB provider
(instead of the internal Access OLEDB provider), i.e. instead of the
line:
cmdCommand.ActiveConnection = CurrentProject.AccessConnection
use the following:
cmdCommand.ActiveConnection = CurrentProject.BaseConnectionString
3. If you use at least ADO 2.6, set the NamedParameters property to
True (before setting the ActiveConnection property) and:
a) use Parameters.Refresh (after setting the ActiveConnection) and fill
only the input parameters you need with something like this:
cmd.Parameters("@.ParameterName")=Value
after cmd.Execute, you can get the value of the output parameter by
reffering to it the same way:
Variable=cmd.Parameters("@.ParameterName")
or:
b) do not use Parameters.Refresh, but add the input parameters that you
need (in any order) (it's not necessary to add the input parameters
that have default values) , and also the output parameters (before
setting the ActiveConnection), with something like this:
cmd.Parameters.Append cmd.CreateParameter("@.ParameterName",...)
after cmd.Execute, you can get the value of the output parameter by
reffering to it the same way as above:
Variable=cmd.Parameters("@.ParameterName")
For more informations about the last method, see:
http://msdn.microsoft.com/library/e...dparameters.asp
The easiest method would be the second one (using the
BaseConnectionString to open another connection).
Note: in this message, I wrote about the case when the procedure has
more input parameters, some of them having default values, in addition
to the output parameters. In your case, if you have just one (output)
parameter, some of my remarks do not apply (i.e. adding the parameters
in the same order that they are defined, etc).
Razvan
expects parameter......parameterized query
Hi all,
I am using the below parameterized query and get an error while executing it...can anyone please spot the error. Any help will be appreciated. I have gone cross-eyed now looking at it all day. The error I get it is
Parameterized Query '(@.Re_UK_Eligible nvarchar(4000),@.Re_Aus_Eligible nvarchar(33),@.R' expects parameter @.Re_JobType_Temp, which was not supplied.
sqlStmt ="UPDATE Re_Users SET Re_UK_Eligible=@.Re_UK_Eligible,Re_Aus_Eligible=@.Re_Aus_Eligible,Re_Can_Eligible=@.Re_Can_Eligible,Re_USA_Eligible=@.Re_USA_Eligible,Re_Address1=@.Re_Address1,Re_Address2=@.Re_Address2,Re_Address3=@.Re_Address3,Re_City=@.Re_City,Re_Postcode=@.Re_Postcode,Re_Country=@.Re_Country,Re_Homephone=@.Re_Homephone,Re_Mobile=@.Re_Mobile,Re_JobType_Per=@.Re_JobType_Per,Re_JobType_Temp=@.Re_JobType_Temp,Re_JobType_Con=@.Re_JobType_Con,Re_Hours_Full=@.Re_Hours_Full,Re_Hours_Part=@.Re_Hours_Part,Re_Sector=@.Re_Sector,Re_StepTwoDone=1 WHERE Re_UserCount=" + Session["ReUserIdentity"]; cn =new SqlConnection(ConfigurationManager.ConnectionStrings["ReConnectionString"].ConnectionString); cmd =new SqlCommand(sqlStmt, cn); cmd.CommandType = CommandType.Text;//Insert UKif (chkUK.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_UK_Eligible", DBNull.Value)); }if ((chkUK.Checked ==true) && (UKRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_UK_Eligible", UKRadioButtonList.SelectedItem.Text)); }//Insert AUSif (chkAUS.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Aus_Eligible", DBNull.Value)); }if ((chkAUS.Checked ==true) && (AUSRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_Aus_Eligible", AUSRadioButtonList.SelectedItem.Text)); }//Insert CANif ((chkCAN.Checked ==false)) { cmd.Parameters.Add(new SqlParameter("@.Re_Can_Eligible", DBNull.Value)); }if ((chkCAN.Checked ==true) && (CANRadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_Can_Eligible", CANRadioButtonList.SelectedItem.Text)); }//Insert USAif (chkUSA.Checked ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_USA_Eligible", DBNull.Value)); }if ((chkUSA.Checked ==true) && (USARadioButtonList.SelectedIndex > -1)) { cmd.Parameters.Add(new SqlParameter("@.Re_USA_Eligible", USARadioButtonList.SelectedItem.Text)); }//Contact Details cmd.Parameters.Add(new SqlParameter("@.Re_Address1", Address1TextBox.Text));if (Address2TextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Address2", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Address2", Address2TextBox.Text)); }if (Address3TextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Address3", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Address3", Address3TextBox.Text)); } cmd.Parameters.Add(new SqlParameter("@.Re_City", CityTextBox.Text)); cmd.Parameters.Add(new SqlParameter("@.Re_Postcode", PostcodeTextBox.Text)); cmd.Parameters.Add(new SqlParameter("@.Re_Country", CountryDropDownList.SelectedItem.Text));if (HomeTelephoneTextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Homephone", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Homephone", HomeTelephoneTextBox.Text)); }if (MobileTelephoneTextBox.Text =="") { cmd.Parameters.Add(new SqlParameter("@.Re_Mobile", DBNull.Value)); }else { cmd.Parameters.Add(new SqlParameter("@.Re_Mobile", MobileTelephoneTextBox.Text)); }//Job Preferencesfor (int i = 0; i < JobTypeCheckBoxList.Items.Count; i++) {if (JobTypeCheckBoxList.Items[i].Text =="Permanent" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Per", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Permanent" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Per", 0)); }if (JobTypeCheckBoxList.Items[i].Text =="Temporary" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Temp", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Temporary" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Temp", 0)); }if (JobTypeCheckBoxList.Items[i].Text =="Contract" && JobTypeCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Con", 1)); }else if (JobTypeCheckBoxList.Items[i].Text =="Contract" && JobTypeCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_JobType_Con", 0)); } }//Hours and Sectorfor (int i = 0; i < HoursCheckBoxList.Items.Count; i++) {if (HoursCheckBoxList.Items[i].Text =="FullTime" && HoursCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Full", 1)); }else if (HoursCheckBoxList.Items[i].Text =="FullTime" && HoursCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Full", 0)); }if (HoursCheckBoxList.Items[i].Text =="PartTime" && HoursCheckBoxList.Items[i].Selected ==true) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Part", 1)); }else if (HoursCheckBoxList.Items[i].Text =="PartTime" && HoursCheckBoxList.Items[i].Selected ==false) { cmd.Parameters.Add(new SqlParameter("@.Re_Hours_Part", 0)); } } cmd.Parameters.Add(new SqlParameter("@.Re_Sector", SectorDropDownList.SelectedItem.Text)); cn.Open(); cmd.ExecuteNonQuery();
thanks,
vijay
I would add the parameters to the collection first and set the value in the IF loops. Sometimes there are other conditions that you might not have accounted for.
Example:
myCommand.Parameters.Add(New SqlParameter("@.userid",SqlDbType.int));if txtbox1.text ==""{myCommand.Parameters("@.userid").Value = 0}else{myCommand.Parameters("@.userid").Value = userid} Also, I would do a response.write of JobTypeCheckBoxList.Items.Count to see what the count is.
|||Gave my sore eyes some rest and changed the code as recommended and guess what it did do the trick!!!
thanks
vijay
Expects parameter which was not supplied....need help....
Not so sure I coded it correctly. And I got this error trying to insert new record
" Procedure 'sp_insert' expects parameter '@.mid', which was not supplied. "
Isn't it i have include @.mid in my code ...??
<STORED PROCEDURES>
sp_insert
(
@.mid char(10),
@.status char(10),
@.name char(20),
@.dj char(20),
@.rank char(20),
@.nric char(14),
@.dob char(20),
@.age char(10),
@.add char(80),
@.school char(50),
@.hp char(10),
@.mp char(10),
@.email char(30),
@.pgname char(20),
@.relationship char(20),
@.contact char(10)
)
AS
insert into Table1(mid,status,name,date_joined,rank,nric,dob,age,address,school,house_phone,Mobile_phone,email,[P/G_name],[P/G_relationship],[P/G_contacts])
values (@.mid,@.status,@.name,@.dj,@.rank,@.nric,@.dob,@.age,@.add,@.school,@.hp,@.mp,@.email,@.pgname,@.relationship,@.contact)
insert into Table2 (name) values (@.name)
RETURN
<WEB FORM>
mycommand = New SqlCommand("sp_insert", mycon)
mycommand.CommandType = CommandType.StoredProcedure
Dim midpar As New SqlParameter("@.mid", SqlDbType.Char, 10)
midpar.Direction = ParameterDirection.Input
midpar.Value = Tb1.Text
Dim statuspar As New SqlParameter("@.status", SqlDbType.Char, 10)
statuspar.Direction = ParameterDirection.Input
statuspar.Value = ddl1.SelectedItem.Text
---
---
---Your webform code is wrong, needs to be something like this:
Dim sqlPar As SqlParameter= myCommand.Parameter.Add("@.mid", SqlDbType.Char, 10)
sqlPar.Direction = ParameterDirection.Input
sqlPar.Value = Tb1.TextsqlPar = myCommand.Parameter.Add("@.status", SqlDbType.Char, 10)
sqlPar.Direction = ParameterDirection.Input
sqlPar.Value = ddl1.SelectedItem.Text
etc.
etc.
etc.