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

asp.net-mvc c# entity-framework

Question

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?

1
8
7/3/2015 7:58:53 AM

Accepted Answer

You need to pass SqlInt32.Null instead of null as follows:

new SqlParameter("MerchantID", MerchantID ?? SqlInt32.Null)
18
7/3/2015 8:31:36 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow