Insert Null value to the Integer Column

22,859

Solution 1

if you wish to do it POP.JobOrderID should be type int? (nullable int) not int

Solution 2

kleinohad is correct. Furthermore, you should assign null, not DBNull.Value

Solution 3

Use Nullable<Int32> or just an alias: int?.

POP.JobOrderID = 5;
// or
POP.JobOrderID = null;

Usage in ADO.NET:

command.Parameters.AddWithValue("@JobOrderId", POP.JobOrderID ?? DBNull.Value);

which is equals to:

POP.JobOrderID.HasValue ? POP.JobOrderID.Value : DBNull.Value;

Solution 4

This

   POP.JobOrderID = new Nullable<Int32>();

should work too IF the JobOrderID is a nullable type (int?).

Cheers

Share:
22,859
thevan
Author by

thevan

Software Engineering Senior Analyst at Accenture Solutions Private Limited, Chennai, India. Interested in ASP.Net, MVC, Web API, WCF, Web Services, ADO.Net, C#.Net, VB.Net, Entity Framework, MS SQLServer, Angular.js, JavaScript, JQuery, Ajax, HTML and CSS

Updated on July 09, 2022

Comments

  • thevan
    thevan almost 2 years

    Through the front end I want to insert NULL value to the Column whose DataType is Int.

    I used like this:

    POP.JobOrderID = Convert.ToInt32(DBNull.Value);
    

    But I cannot Insert Null value, it throws error such as "Object cannot be cast from DBNull to other types":

    How to insert NULL values?

  • thevan
    thevan almost 13 years
    POP is an Object and JobOrderID is an Int. How to assign like this?
  • thevan
    thevan almost 13 years
    I am using three tier architecture. I would pass parameter like above what i had given in the Question. How to apply int?(nullable) in this?
  • thevan
    thevan almost 13 years
    I am using three tier architecture. I would pass parameter like above what i had given in the Question. How to apply int?(nullable) in this?