Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert null into sql database with ADO.Net C#

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;
}
like image 462
Mohsen Safari Avatar asked Mar 24 '13 15:03

Mohsen Safari


People also ask

How do I insert a NULL record in SQL?

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);

Can we put NULL in SQL?

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'.

How do you handle database NULL values in C#?

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).

How do you define NULL in SQL?

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.


1 Answers

Use System.DBNull class:

CmdUpdate.Parameters["@SubsetID"].Value = DBNull.Value;
like image 70
MarcinJuraszek Avatar answered Nov 05 '22 17:11

MarcinJuraszek