Insert Null value to the Integer Column
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
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, 2022Comments
-
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 almost 13 yearsPOP is an Object and JobOrderID is an Int. How to assign like this?
-
thevan almost 13 yearsI 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 almost 13 yearsI 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?