参数将错误从存储过程传递到asp.net

时间:2014-01-09 05:58:13

标签: c# asp.net sql-server-2008 stored-procedures

我在Datalayer中编写了一个函数,并在业务层中访问,它在业务层中抛出一个异常Procedure or function 'create_UR_Map' expects parameter '@User_Id', which was not supplied.

 I have showed the code below

1)数据层

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)业务层

 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)存储过程

  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)表

    User_Role_Map
     User_I      varchar(15)
Role_Id      int    

我已经搜索了错误并发现如果传递的参数不相等则会出现上述错误。但是我已经传递了所有参数仍然是它的例外情况。非常感谢

1 个答案:

答案 0 :(得分:1)

由于您正在调用存储过程,因此请删除数据层中的以下代码。

Cmd.CommandType = CommandType.Text;