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

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
codeoxygenAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
int? po
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Dbnull.value
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
codeoxygenAuthor Commented:
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
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
codeoxygenAuthor Commented:
example for declaring as nullable
0
 
Carl TawnSystems and Integration DeveloperCommented:
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?.
0
 
Alan WarrenApplications DeveloperCommented:
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.
CREATE PROCEDURE dbo.my_proc
    @po int = NULL,  -- NULL default value
    @mo int = 2,    -- Default value of 2
    @ko int = 3      -- Default value of 3
AS 
    SET NOCOUNT ON;
    SELECT @po, @mo, @ko;
GO

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

Open in new window

Respectfully yours,
Alan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.