1

This is driving me nuts ;)

I have this stored procedure ...

ALTER PROCEDURE [dbo].[sproc_FindFoundries] 
    (@materials varchar(1000),
     @capabilities varchar(1000))
AS
BEGIN

 /* insert SQL code here */

END

The procedure accepts two comma delimited strings. In my application I have the following code.

BCDataContext db = new BCDataContext();
SqlParameter prmMaterials = new SqlParameter("materials", SqlDbType.VarChar, 1000);
prmMaterials.Value = materialList;
SqlParameter prmCapability = new SqlParameter("capabilities", SqlDbType.VarChar, 1000);
prmCapability.Value = capabilityList;

SqlConnection cn = new SqlConnection(db.Connection.ConnectionString);
SqlCommand cmd = new SqlCommand("sproc_FindFoundries", cn);
cmd.Parameters.Add(prmMaterials);
cmd.Parameters.Add(prmCapability);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

When I execute the code, I get the error

Procedure or function [sproc_name] expects parameter '@materials', which was not supplied.

when I try fill the dataset. When testing I have verified that both parameters contain data and are not null. Is there something I've missed? A second pair of eyes would be greatly appreciated.

Thanks.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Andy Evans
  • 6,997
  • 18
  • 72
  • 118

4 Answers4

7

Use @materials, @capabilities as parameters' name:

using (BCDataContext db = new BCDataContext())
using (SqlConnection connection =  new SqlConnection(db.Connection.ConnectionString))
using (SqlCommand command = connection.CreateCommand())
{
    command.CommandText = "sproc_FindFoundries";
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.Add("@materials", SqlDbType.VarChar, 1000).Value = materialList;
    command.Parameters.Add("@capabilities", SqlDbType.VarChar, 1000).Value =  capabilityList;

    DataSet ds = new DataSet();
    using (SqlDataAdapter da = new SqlDataAdapter(command))
    {
        da.Fill(ds);
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    I've been using LINQ and Entity Framework way too long. I forgot to specify the commandtype. I'm an idiot, Lol. ;) – Andy Evans Feb 04 '11 at 19:58
4

When naming your parameters you need to put the @ in

SqlParameter prmMaterials = new SqlParameter("@materials", SqlDbType.VarChar, 1000)
Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
2

You are calling your parameters "materials" and "capabilities" instead of "@materials" and "@capabilities"

Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
1

I tried like this, Its worked for me:

int deal_id = 25;
_dbContext.Database.ExecuteSqlCommand("exec sp_getdeal @deal_id={0}", deal_id);

My procedure is like this:

ALTER PROCEDURE [dbo].[sp_getdeal]
(
    @deal_id INTEGER
)
AS
BEGIN
Perception
  • 79,279
  • 19
  • 185
  • 195
Nalan Madheswaran
  • 10,136
  • 1
  • 57
  • 42