7

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?

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

POP is an Object and JobOrderID is an Int. How to assign like this?
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?
5

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

1 Comment

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?
4

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;

Comments

2

This

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

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

Cheers

Comments

0

Like this:

 POP.JobOrderID = null;    // JobOrderID should be of type int? which is a nullable-int

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.