i have a DropDownList that display a collection of subset based on SetID from another ddl and some times it is null. how i can insert null value into database ? i tried with this code but not work:
CmdUpdate.Parameters.Add("@SubsetID",SqlDbType.Int);
if (ddlSubset.Items.Count!=0)
{
CmdUpdate.Parameters["@SubsetID"].Value=ddlSubset.SelectedValue;
}
esle
{
CmdUpdate.Parameters["@SubsetID"].Value=null;
}
You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);
SQL allows any datatype to have a NULL value. This isn't the same as a blank string or a zero integer. It boils down to the meaning 'Unknown'.
IsNullOrEmpty() Method of C# This method is used when you want to check whether the given string is Empty or have a null value or not? If any string is not assigned any value, then it will have Null value. The symbol of assigning Null value is “ “or String. Empty(A constant for empty strings).
A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value. Note: A NULL value is different from a zero value or a field that contains spaces.
Use System.DBNull
class:
CmdUpdate.Parameters["@SubsetID"].Value = DBNull.Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With