Is there a way to pass a null value to a parameter in C#?
Reason is I have to pass some sql parameters as null values (Default values are set in the stored procedure) to some stored procedures in my SQL database. My code behind is receiving data through a data-access class. Therefore, I have to do lot of method overloadings in the data-access class to support code behind events/methods. Ex;
public static DataTable GetUsers(int companyId, int departmentId){
//Return a DataTable of users in the given company & dept
}
public static DataTable GetUsers(int companyId){
//Return a DataTable of users in the given company
}
If I could pass a null value to departmentId I can avoid this overloading and let the sp handle it.
Cheers !!
In the first line
myIntdefined as aNullableorNullable of int. They are instances of theSystem.Nullablestruct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.By defining method parameter as above will allow to pass a null value. Ex;
Hope this helps.