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

Posted on 2014-04-25
Last Modified: 2014-04-29
how to pass sql parameter as null value in to integer data type variable

po =txt_PONo_int.Text == "" ? 0 : Convert.ToInt32(txt_PONo_int.Text);

how zero should be replaced , i want save it as null instead of  zero
Question by:codeoxygen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40022301
LVL 52

Expert Comment

by:Carl Tawn
ID: 40022354
It depends what po is. If it's a Nullable<int> then you set it to null. The translation to DbNull should come when you are assigning the value to a parameter on a command object.

Author Comment

ID: 40022416
o.po  o is a entity class object PO is variable of that class , whose value is int

in entity class po is declared as

public int po {get;set;}

can help me how to declare it if i want to pass null value to DB sql server
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40022453
int cannot accept NULL or DBNull.Value, as far as I know, so you will have to put some different code later, where po is used in the db query.
LVL 52

Expert Comment

by:Carl Tawn
ID: 40022466
Is po defined as allowing null in your database table? It may just be that it hasn't filtered through to your entity model.

Edit the model and look at the settings for the po column. If "Nullable" is set to "(None)" then change it to true. But it needs to be nullable in the underlying table too.

Author Comment

ID: 40023053
example for declaring as nullable
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 40023107
int? po
LVL 52

Expert Comment

by:Carl Tawn
ID: 40023483
If you're building your entity model from a database then you won't specify they type directly; it will be picked up from the underlying columns data type.

But yes, it would appear as int?.
LVL 26

Expert Comment

by:Alan Warren
ID: 40024170
Hi codeoxygen,

I think you can just give the SQL Stored Procedure Parameter a default value of NULL, by doing so the input param becomes optional; then don't pass that param when you execute the stored procedure from your code. Sorta the same effect as setting the ConvertEmptyStringToNull property in the SQL Parameter definition of a SqlDataSource object.
    @po int = NULL,  -- NULL default value
    @mo int = 2,    -- Default value of 2
    @ko int = 3      -- Default value of 3
    SELECT @po, @mo, @ko;

-- exec dbo.my_proc    -- No parameters supplied
-- Returns:
-- NULL  2  3

Open in new window

Respectfully yours,

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query 5 44
TSQL - How to get output results in row with additional character(comma) 7 16
add stored proc on publlication 4 20
Estimating my database size 7 21
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question