Question:
What is the drop-down list that allows blank or null to be selected

Solution

You can allow users to select a null or a blank value from a drop-down list by adding an empty List Item to the DropDownList. The List Item can have an empty string as its value, and you can set the Text property to represent the blank or null option.


There are certain things that you need to consider:


Thus, you need to handle with the scenario:


For example, if you keep @ContactID as a null/empty string, you need to pass the value as DBNull.Value.


When a @ContactID has value, you must cast it into an integer.


cmd.Parameters.AddWithValue("@ContractID ", String.IsNullOrEmpty(ContractID)

    ? (object)DBNull.Value

    : Convert.ToInt32(ContractID));


More importantly, you need to provide the parameter with value and type (length) instead of just providing the value via AddWithValue(). You may read the article: >AddWithValue is Evil


cmd.Parameters.Add("@ContractID", SqlDbType.Int).Value = String.IsNullOrEmpty(ContractID)

    ? (object)DBNull.Value

    : Convert.ToInt32(ContractID);


Also, make sure that your stored procedure is modified with the ContractID parameter to allow null. As it is nullable and optional, the parameter needs to be placed at the end (mandatory/not nullable parameter(s) comes first).


CREATE PROCEDURE dbo.spNewEmployeeList

  -- Other Parameters,

  ContractID INT = NULL


Answered by: >Yong Shun

Credit: >StackOverflow


Suggested Blogs

>Creating a pivot table by 6 month interval rather than year

>How remove residual lines after merge two regions using Geopandas in python?

>How to mock a constant value in Python?

>Can I call an API to find out the random seed in Python `hash()` function?

>How to configure python in jmeter?

>How to fix routes in Laravel?


Submit
0 Answers