Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public class Service1 : IService1
    {

        string IService1.create(Class1 bobj)
        {
            string userid = string.Empty;

            SqlConnection con = new SqlConnection("conn");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_UserDetails", con);
            {
                cmd.CommandType = CommandType.StoredProcedure;
               
                cmd.Parameters.Add(new SqlParameter("UserName", bobj.UserName));
                cmd.Parameters.Add(new SqlParameter("Password", bobj.Password));
                cmd.Parameters.Add(new SqlParameter("Country", bobj.Country));
                cmd.Parameters.Add(new SqlParameter("Email", bobj.Country));
                cmd.ExecuteNonQuery();
            }
            return userid;
        }

i got this error: near the ExecutiveNonQuery "Procedure or function 'sp_UserDetails' expects parameter '@UserId', which was not supplied."
SQL
ALTER PROCEDURE [dbo].[sp_UserDetails]
(
    
    @UserName	nvarchar(100),
    @Password   nvarchar(50),
    @Country	nvarchar(50),
    @Email		nvarchar(100),
    @UserId		int OUTPUT
    
)
AS

INSERT INTO RegistrationTable
(
    UserName,
    Password,
    Country,
    Email
    
)

VALUES
(
    @UserName,
    @Password,
    @Email,
    @Country
    
)

SELECT
    @UserId = @@Identity
Posted
Updated 29-Oct-12 18:02pm
v2

Simple,your procedure requires '@UserId' parameter which is missing in the code. you need to add another parameter like below,


public class Service1 : IService1
    {
 
        string IService1.create(Class1 bobj)
        {
            string userid = string.Empty;
 
            SqlConnection con = new SqlConnection("conn");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_UserDetails", con);
            {
                cmd.CommandType = CommandType.StoredProcedure;
               
                cmd.Parameters.Add(new SqlParameter("@UserName", bobj.UserName));
                cmd.Parameters.Add(new SqlParameter("@Password", bobj.Password));
                cmd.Parameters.Add(new SqlParameter("@Country", bobj.Country));
                cmd.Parameters.Add(new SqlParameter("@Email", bobj.Email));
                cmd.Parameters.Add(new SqlParameter("@UserId", bobj.UserId));
                cmd.ExecuteNonQuery();
            }
            return userid;
        }
 
Share this answer
 
v2
As Per the Above Code You Have To Pass 5 Parameters While Executing the Procedure
1 : UserName
2 : Password 
3 : Country
4 : Email
5 : UserId

In Your C# Code Your are Passing Only 4 Parameters while Executing the Procedure See the Below Code :
C#
SqlConnection con = new SqlConnection("conn");
con.Open();
SqlCommand cmd = new SqlCommand("sp_UserDetails", con);
{
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("UserName", bobj.UserName));
    cmd.Parameters.Add(new SqlParameter("Password", bobj.Password));
    cmd.Parameters.Add(new SqlParameter("Country", bobj.Country));
    cmd.Parameters.Add(new SqlParameter("Email", bobj.Email));
    cmd.Parameters.Add(new SqlParameter("UserId", bobj.UserId));
    cmd.ExecuteNonQuery();
}

Or Else Remove the UserID Parameter From Your Procedure If Not Required.
SQL
ALTER PROCEDURE [dbo].[sp_UserDetails]
(
    
    @UserName	nvarchar(100),
    @Password   nvarchar(50),
    @Country	nvarchar(50),
    @Email      nvarchar(100)
    
)
AS
 
INSERT INTO RegistrationTable
(
    UserName,
    Password,
    Country,
    Email
    
)
 
VALUES
(
    @UserName,
    @Password,
    @Email,
    @Country
    
)
 
SELECT
    @@Identity
 
Share this answer
 
v2
Comments
2011999 30-Oct-12 0:36am    
ALTER PROCEDURE [dbo].[sp_displaydata]
(
@UserId int
)
AS

SELECT
UserName,
Password,
Country,
Email


FROM RegistrationTable

WHERE
UserId = @UserId
Procedure or function 'sp_UserDetails' expects parameter '@UserId', which was not supplied."


List<class1> IService1.display()
{
List<class1> Classlist = new List<class1>();

Class1 objclass = null;
SqlDataReader reader = null;
SqlConnection con = new SqlConnection("conn");
con.Open();
SqlCommand cmd = new SqlCommand("sp_displaydata", con);
{
cmd.CommandType = CommandType.StoredProcedure;


reader = cmd.ExecuteReader();
while (reader.Read())
{
objclass = new Class1();
objclass.UserId = Convert.ToString(reader["UserId"]);
objclass.UserName = Convert.ToString(reader["UserName"]);
objclass.Password = Convert.ToString(reader["Password"]);
objclass.Country = Convert.ToString(reader["Country"]);
objclass.Email = Convert.ToString(reader["Email"]);
Classlist.Add(objclass);
}
reader.Close();
return Classlist;
}
}
Manoj K Bhoir 31-Oct-12 3:07am    
In Your Above Procedure It Requires Only One Parameter @UserId and Your Are Not Passed this Parameter From Your C# Code. Just Write Following Code below
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("@UserId", bobj.UserId));
Manoj K Bhoir 31-Oct-12 3:10am    
Don't Forget to Hit 5 :)
Hey man you must have to write your code as follows.
You should have to write '@' prefix of parameter name.
C#
public class Service1 : IService1
    {
 
        string IService1.create(Class1 bobj)
        {
            string userid = string.Empty;
 
            SqlConnection con = new SqlConnection("conn");
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_UserDetails", con);
            {
                cmd.CommandType = CommandType.StoredProcedure;
               
                cmd.Parameters.Add(new SqlParameter("@UserName", bobj.UserName));
                cmd.Parameters.Add(new SqlParameter("@Password", bobj.Password));
                cmd.Parameters.Add(new SqlParameter("@Country", bobj.Country));
                cmd.Parameters.Add(new SqlParameter("@Email", bobj.Country));
                cmd.ExecuteNonQuery();
            }
            return userid;
        }
 
Share this answer
 
v3
Hi.. you have an out parameter to get the user id to WCF service class. So you need to add UserId parameter to the command object as an output parameter. After calling the cmd.ExecuteNonQuery(), we need to get the value of out parameter of the stored procedure. See this code block

 string userid = string.Empty;

 SqlConnection con = new SqlConnection("conn");
 con.Open();
 SqlCommand cmd = new SqlCommand
 {
     CommandText = "sp_UserDetails",
     Connection = con,
     CommandType = CommandType.StoredProcedure
 };

SqlParameter[] objSqlParameters = new SqlParameter[]
      {
          new SqlParameter("UserName",bobj.UserName),
          new SqlParameter("Password",bobj.Password),
          new SqlParameter("Country",bobj.Country),
          new SqlParameter("Email",bobj.Email),
          new SqlParameter("@UserId",SqlDbType.Int)
          {
             Direction = ParameterDirection.OutPut
          }
      }
 try
{
 con.Open();
 cmd.ExecuteNonQuery();

 userid = cmd.Parameters["@UserId"].Value;
 return userid;
 }
 catch(Exception ex)
 {
   //log your exception
 }
 finally
 {
  con.Close();
 }


Thank you
 
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