简体   繁体   中英

How to pass null value into store procedure with entity framework?

I am using mvc application with the entityframework. I want to pass null values parameter to store procedure. I want to pass MerchantID as null, also sometime MerchantID has value.

GetValues(int[] TicketID,int? MerchantID,bool IsOpen)
{

//TicketID has values 1123,1122 etc
//MerchantID sometimes null
//IsOpen true/false

  DataTable tbldepartmentid = new DataTable("Departmentid");
             tbldepartmentid.Columns.Add("VALUE", typeof(int));
            foreach (var id in TicketID)
                tbldepartmentid.Rows.Add(id);



 List<GetTroubleTicketDetails_Result> GetTroubleTicketDetails = _getTroubleTicketDetails_Result.ExecuteCustomStoredProc("Tickets.GetDetails", " @GroupID,@MerchantID,@Open",
                 new SqlParameter("GroupID", SqlDbType.Structured) { Value = tbldepartmentid, TypeName = "dbo.tblTVPForCSVINT" }
                 , new SqlParameter("MerchantID", MerchantID)
                 , new SqlParameter("Open", IsOpen)).ToList();
                return GetTroubleTicketDetails;

}

my problem is when I pass MerchantID=null then it gives me below error

"The parameterized query '(@GroupID [dbo].[tblTVPForCSVINT] READONLY,@MerchantID nvarchar(' expects the parameter '@MerchantID', which was not supplied."

How to pass null value for MerchantID?

您需要传递SqlInt32.Null而不是null ,如下所示:

new SqlParameter("MerchantID", MerchantID ?? SqlInt32.Null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM