Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass sql parameter as null value in integer datatype variable?

how to pass sql parameter as null value in to integer data type variable ?

        StockBO sBO = new StockBO();
        sBO.Mode = 2;
        if (ddcmpanyname.SelectedIndex != 0)
        {
            sBO.Client_id = Convert.ToInt16(ddcmpanyname.SelectedValue);
        }
        else
        {
            sBO.Client_id = Convert.ToInt16(DBNull.Value);//Object cannot be cast from DBNull to other types.
       sBO.Client_id =DBNull.Value;//Cannot implicitly convert type 

        }
        ds=_StockController.StockGet(sBO);

i changed the code like this it gives error like i my comment check else part

like image 710
Ayyappan Anbalagan Avatar asked Jul 15 '10 05:07

Ayyappan Anbalagan


People also ask

Can SQL integer be NULL?

SQL also has NULL which means "Unknown". A NULL value can be set. NULL is a non-value, so it can be assigned to TEXT columns, INTEGER columns or any other datatype.

How do I assign a NULL value to a variable in SQL?

How To Assign NULL Values to Variables or Columns? The rule for assigning NULL values to variables or table columns is simple: Use keyword "NULL" directly as normal values. Specificly, "NULL" can be used in SET statements to assign NULL values to variables.

How do you pass a parameter as NULL?

You can't pass a primitive as null. There's two ways to do this- first is to make the function take Double- the class version of a double. The other is to pass in some unused value, such as -1, to mean null. That requires there to be a reasonable default value though.

How do I allow NULL values in SQL query?

ALTER TABLE table_name ALTER COLUMN column_name DATA_TYPE [(COLUMN_SIZE)] NULL; In this syntax: First, specify the name of the table from which you want to change the column. Second, specify the column name with size which you want to change to allow NULL and then write NULL statement .


1 Answers

Try this,

else
    {
        sBO.Client_id = System.Data.SqlTypes.SqlInt32.Null;
    }
like image 121
ACP Avatar answered Oct 07 '22 23:10

ACP