简体   繁体   中英

stored procedure parameter error c#

I am creating a website in which i need to call a stored procedure but the following error message is showing

Procedure expects parameter which was not supplied

My C# code:

SqlCommand md = new SqlCommand("SPSelcsclass");  // select charge,shortclass from class where class=@class  
md.CommandType = CommandType.StoredProcedure;
md.Connection = con;

SqlParameter paam;
paam = new SqlParameter("@class", "SLEEPER CLASS");
paam.Direction = ParameterDirection.Input;
paam.DbType = DbType.String;
cmd.Parameters.Add(paam);

con.Open();

SqlDataReader sdr = md.ExecuteReader();

while (sdr.Read())
{
    lb11.Text = sdr["charge"].ToString();
    lb2.Text = sdr["shortclass"].ToString();
}

SQL Server stored procedure:

create procedure SPSelcsclass  
    @class nvarchar(500)  
as  
begin  
    select charge, shortclass 
    from class 
    where class = @class  
end

Error:

Procedure or function 'SPSelcsclass' expects parameter '@class', which was not supplied.

Here is your offending line (marked up with ** below). It seems you have two commands and you are setting the parameter to command cmd, not md. Replace cmd.Parameters.Add(paam); with md.Parameters.Add(paam);

SqlCommand md = new SqlCommand("SPSelcsclass");//select charge,shortclass from class where class=@class  
                md.CommandType = CommandType.StoredProcedure;
                md.Connection = con;
                SqlParameter paam;
                paam = new SqlParameter("@class", "SLEEPER CLASS");
                paam.Direction = ParameterDirection.Input;
                paam.DbType = DbType.String;
                **cmd.Parameters.Add(paam);**
                con.Open();
                SqlDataReader sdr = md.ExecuteReader();
                while (sdr.Read())
                {
                    lb11.Text = sdr["charge"].ToString();
                    lb2.Text = sdr["shortclass"].ToString();
                }

You create two different SqlCommand objects in your code and you only need one. That results in you adding parameters to a different command than the one you are calling.

You are calling SqlCommand cmd yet you are adding parameters to an SqlCommand md which we dont even know where it comes from.

The way you create the parameter is quiet strange to me. I believe you could omit quiet a lot of it and simplyfy all of the code to this:

            using (SqlConnection connection = con)
        {
            SqlCommand command = new SqlCommand("SPSelcsclass", connection);  // select charge,shortclass from class where class=@class  
            command.Parameters.AddWithValue("class", "SLEEPER CLASS");

            connection.Open();

            SqlDataReader sdr = command.ExecuteReader();
            while(sdr.Read())
            {
                lb11.Text = sdr["charge"].ToString();
                lb2.Text = sdr["shortclass"].ToString();
            }

            connection.Close();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM