Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this is a very basic issue, but for some reason this is eluding me.. I keep getting the following error:

" Message=Procedure or function 'GetMembersOfAMemberType' expects parameter '@MemberType', which was not supplied."

I am able to execute the SProc in SQL Server.
Here is the code:

C#
public DataSet GetMembersByType(int memID)
        {
            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(cnn);
            con.Open();
            SqlCommand cmd = new SqlCommand("GetMembersOfAMemberType", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@MemberType";
            param.Value = memID;
            cmd.Parameters.Add(param);
           
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("GetMembersOfAMemberType", con);
                int i = cmd.ExecuteNonQuery(); // for debugging - I always get a -1 here, telling me that the code was not executed.
                da.Fill(ds, "MemTable");
                return ds;
            }
            catch
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            
        }
Posted
Updated 2-Jul-12 15:08pm
v2
Comments
barneyman 2-Jul-12 20:36pm    
don't you have to give the sqlparameter a direction, input/output/retval?

C#
// if you need to check nomber of records using ExecuteNonQuery
using (var con = new SqlConnection(connectionString))
{
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = "GetMembersOfAMemberType";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@MemberType", memID);
        con.Open();
        var ret = cmd.ExecuteNonQuery();
    }
}

// if you need to fill DataTabale using Stored Procedure
using (var con = new SqlConnection(connectionString))
{
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = "GetMembersOfAMemberType";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@MemberType", memID);
        using (var da = new SqlDataAdapter(cmd))
        {
            con.Open();
            DataSet ds = new DataSet();
            da.Fill(ds, "MemTable");
            return ds;
        }
    }
}
 
Share this answer
 
Comments
Gymnast 3-Jul-12 14:26pm    
I appreciate your input, but this seems to be JavaScript code. I am looking for a solution in C#.
[no name] 3-Jul-12 16:35pm    
Looks like C# to me. var is valid in C# also.
Gymnast 3-Jul-12 18:41pm    
Thanks Damith. This works! Strangely I have been able to produce results without the "var" earlier. Can you explain why I needed the var here?
DamithSL 3-Jul-12 22:59pm    
http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp
You need to apply the type of the input parameter.
For e.g. param.DbType = DbType.String;.

Besides you should also mention the Direction i.e. Input or Output.
 
Share this answer
 
Comments
Gymnast 3-Jul-12 14:24pm    
Added this and still get the same error.
Maybe this will help you guys understand this better..

Source code:

XML
<asp:ObjectDataSource ID="odsGVMembers" runat="server"
         SelectMethod="getMemtype" TypeName="WCSMemberManagement.BAL">
         <SelectParameters>
             <asp:ControlParameter ControlID="ddlMemberType" DefaultValue="9" Name="MemID"
                 PropertyName="SelectedValue" Type="Int32" />
         </SelectParameters>
     </asp:ObjectDataSource>


Stored Procedure:

ALTER PROCEDURE [dbo].[GetMembersOfAMemberType]

@MemberType INT

AS
BEGIN

SET NOCOUNT ON;

SELECT

m.MemberID
, m.FirstName
, m.LastName
, m.MemberTypeID
, mt.MemberTypeDescription
FROM dbo.tblMember m
INNER JOIN dbo.lkpMembershipType mt
ON m.MemberTypeID = mt.MemberTypeID
WHERE m.MemberTypeID = @MemberType
END
 
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