Alex Rivera | Logout

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

Asked 2010-07-15T05:53:28.420
9

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

Edit
Report

3 Answers

22

Try this,

else
    {
        sBO.Client_id = System.Data.SqlTypes.SqlInt32.Null;
    }
answered 2010-07-15T06:56:27.887
1

from SQL to C# >

int number;
Int32.TryParse(paramvalue, out number);

from C# to SQL >

add DBNull to your parameter

answered 2010-07-15T05:58:41.890
0

Considering ParamValue contains the result of the query as an integer or a DBNull.Value, I'm casting it to int? this way:

var Result = ParamValue as int?;

To determine whether Result is null, do check Result.HasValue. Otherwise, use it as a usual integer, or Result.Value

answered 2010-07-15T06:06:30.120

Your Answer