Click here to Skip to main content
15,909,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi.
I intend to read number of rows in DB and list them inside DropDownList, so for this issue i have written the following class:
C#
public static DataSet ExecuteDataSet(string CMDText, CommandType CMDType)
       {
           SqlConnection conn = new SqlConnection(ConnectionString);
           SqlCommand cmd = conn.CreateCommand();
           cmd.CommandText = CMDText;
           cmd.CommandType = CMDType;

           DataSet ds = new DataSet();
           SqlDataAdapter sad = new SqlDataAdapter(cmd);

           conn.Open();
           sad.Fill(ds);
           conn.Close();
           conn.Dispose();
           return ds;

       }


and there is a stored procedure in SqlServer 2008r2 as below to read all table's records:
SQL
Create PROCEDURE [dbo].[Select_City_SP](

@CityId int,
@CityName nvarchar(50)
)
as
begin

select CityId,CityName from City

return @@Rowcount
end


and at the end i call them in this code:
C#
if (!IsPostBack)
{
    DataSet Dataset = DataProvider.ExecuteDataSet("[dbo].[Select_City_SP]", CommandType.StoredProcedure);
    {
        CityDrpDown.DataSource=Dataset;
        CityDrpDown.DataTextField="CityName";
        CityDrpDown.DataValueField="CityId";
        CityDrpDown.DataBind();
    }
}


but when i debug the solution this exception appears:
Procedure or function 'Select_City_SP' expects parameter '@CityId', which was not supplied.
what shall i do?
Posted

In your stored proc you mentioned 2 input parameters but its not required ..You are not using it anywhere. so remove it.


SQL
Create PROCEDURE [dbo].[Select_City_SP]
as
begin
 
select CityId,CityName from City

end
 
Share this answer
 
Comments
Santhosh Kumar Jayaraman 15-Aug-12 1:41am    
have u compiled stored proc in database and are you pointing to correct database from application?
Your command expects two parameters CityId and CityName. You could add this in your SqlCommand like this

C#
cmd.Parameters.AddWithValue("@CityId", 1);
cmd.Parameters.AddWithValue("@CityName", "MyCity");


Bu most likely you could just remove these parameters from your procedure:

SQL
ALTER PROCEDURE [dbo].[Select_City_SP]
as
begin
 
select CityId,CityName from City
 
return @@Rowcount
end


as you want it to return all rows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900