Add multiple values to a SqlParameter
-
I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query.
SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) };
I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search)SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") };
What can I do to not include the @Status in the sqlparams if its empty? -
I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query.
SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) };
I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search)SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") };
What can I do to not include the @Status in the sqlparams if its empty?If the stored procedure you are using for your search takes both eventID and Status field then you need to pass both the parameter otherwise always error message will be thrown. My suggestion is either you use different stored procedure for different combination of search criteria or handlle it on the single stored procedure depending upon the value of SQL parameter.
-
I current have code to add the SQL Parameters to a array but when searching I need to sometimes not include the SQLParameter as its not required in the search. A example of my SQL Parameters is below which is passed to a function that runs the query.
SqlParameter[] sqlparams = { new SqlParameter("@EventID", EventID), new SqlParameter("@Status", Status) };
I tried to use the following code but it produces a error if one of the two values is not in the query string (I do not insert the EventID or Status in the query string if its not being search)SqlParameter[] sqlparams = { EventID.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@EventID", "%" + EventID.Trim() + "%"), Status.Trim().Equals("") ? new SqlParameter() : new SqlParameter("@Status", "%" + Status.Trim() + "%") };
What can I do to not include the @Status in the sqlparams if its empty?When you don't need SqlParamater just set its value to DBNULL
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion