Question

I would like to pass a parameter to this stored procedure:

ALTER PROCEDURE [USP_SelectProject]
-- Add the parameters for the stored procedure here

@ProjectNumber as int

AS
BEGIN
if @ProjectNumber is null 
begin
select * from tbl_projects
end
else
begin
SELECT * from tbl_projects where ProjectID = @ProjectNumber
end
End

Here is the .net code

SqlConnection PTConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Project_Tracker"].ConnectionString);
SqlCommand PTCmd = new SqlCommand("USP_SelectProject", PTConn);
PTCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter PTda = new SqlDataAdapter(PTCmd);
PTda.SelectCommand.Parameters["@ProjectNumber"].Value = DBNull.Value;
DataSet PTds = new DataSet();
PTda.Fill(PTds);

        GridView1.DataSource = PTds;
        GridView1.DataBind();

I am receiving the following error: An SqlParameter with ParameterName '@ProjectNumber' is not contained by this

Was it helpful?

Solution

You must Add() the parameter first.

SqlParameter p = PTda.SelectCommand.Parameters.Add("@ProjectNumber", DbType.Int);
p.Value = DBNull.Value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top