Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

c# - How to Pass List<> to stored procedure

I am trying to pass a list to database (SQL Server 2008 R2) using user defined table types but get some errors my code is:

public int DAL_SaveIncramentSalary(tbl_Employee_Master obj, List < dtIncrementSalary > tbl) {
    try {
        SqlParameter[] objSqlParameter = new SqlParameter[4];
        objSqlParameter[0] = new SqlParameter("@Company_ID", obj.Company_ID);
        objSqlParameter[1] = new SqlParameter("@Employee_ID", obj.Employee_ID);
        objSqlParameter[2] = new SqlParameter("@Salary_Month", obj.Govt_DA);
        objSqlParameter[3] = new SqlParameter("@dt", SqlDbType.Structured);
        objSqlParameter[3].Value = tbl;
        objSqlParameter[3].Direction = ParameterDirection.Input;
        DataSet fdd = SqlHelper.ExecuteDataset(_cnnString2, "usp_Insert_Increment_Salary_List", CommandType.StoredProcedure, bjSqlParameter);
        DataSet fddd = SqlHelper.ExecuteDataset(_cnnString2, "usp_Insert_Increment_Salary_List", objSqlParameter);
        DataSet ds = SqlHelper.ExecuteDataset(_cnnString2, CommandType.StoredProcedure, "usp_Insert_Increment_Salary_List", objSqlParameter);
        return SqlHelper.ExecuteNonQuery(_cnnString2, CommandType.StoredProcedure, "usp_Insert_Increment_Salary_List", objSqlParameter);
    } catch (Exception ex) {
        throw new Exception(ex.Message);

I got error like this:

Dataset dff error : Parameter count does not match Parameter Value count. DataSet fddd error : ailed to convert parameter value from a List1 to a IEnumerable1. DataSet ds error :Failed to convert parameter value from a List1 to a IEnumerable1.

SQL Server user defined type

CREATE type dtincrementsalary AS TABLE
    head_id INT NULL, 
    salamt NUMERIC(18,2) NULL, 
    per FLOAT NULL, 
    oldsalamt NUMERIC(18,2) NULL,  
    oldper FLOAT NULL 

PROCEDURE [DBO].[Usp_insert_increment_salary_list] ( @Company_ID  INT, 
                                                    @Employee_ID  INT, 
                                                    @Salary_Month INT, 
                                                    @dt DTINCREMENTSALARY readonly ) 
  FROM   @dt 
See Question&Answers more detail:os

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Issue in your code objSqlParameter[3].Value = tbl;

tbl is List<dtIncrementSalary> but SqlDbType.Structured will take Table

suggest you to convert your List<dtIncrementSalary> in Datatable with same schema as you created in Sql Server(dtincrementsalary)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share