Shreyas Achar
Shreyas Achar

Reputation: 1435

Parameters passing error from stored procedure to asp.net

I have written a function in Datalayer and is accessed in the business layer and it is throwing an exception in business layer as Procedure or function 'create_UR_Map' expects parameter '@User_Id', which was not supplied.

 I have showed the code below

1)Data layer

public int fninsertuser_role_map(UserMaster u, int[] role, int i)
{
    SqlConnection Con = new SqlConnection(str);
    Con.Open();
    transaction = Con.BeginTransaction();
    int result=0;

    for (int a = 0; a < i; a++)
    {
        SqlCommand Cmd = new SqlCommand("create_UR_Map", Con,transaction);
        Cmd.CommandType = CommandType.StoredProcedure;
        Cmd.Parameters.Clear();
        Cmd.Parameters.AddWithValue("@User_Id", u.UserName);
        Cmd.Parameters.AddWithValue("@Role_Id", role[a]);
        Cmd.CommandType = CommandType.Text;

       result = Cmd.ExecuteNonQuery();
    }
    transaction.Commit();
    return result;
}

2)Business layer

 public int fninsertuser_role_map(UserMaster ua,int []role,int i)
 {
    //  Connection connect = new Connection();
    try
    {
        return cs.fninsertuser_role_map(ua, role, i);

    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        cs = null;
    }

}

3)Stored procedure

  USE [RHK_HIS]
  GO
 SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
 ALTER PROCEDURE [dbo].[create_UR_Map]
  @User_Id varchar(15),
  @Role_Id int
   AS
    BEGIN
insert into User_Role_Map (User_Id,Role_Id) values (@User_Id,@Role_Id)  
END

4)table

    User_Role_Map
     User_I      varchar(15)
Role_Id      int    

I have googled the error and found out that if parameters passed are unequal then the above error will come.But I have passed all the parameters still its thrpwing the exception.any help are appreciated

Upvotes: 0

Views: 593

Answers (1)

Adarsh Shah
Adarsh Shah

Reputation: 6775

Remove following code in your data layer since you are calling a stored procedure.

Cmd.CommandType = CommandType.Text;

Upvotes: 1

Related Questions