[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-04-25
9
Medium Priority
?
2,706 Views
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
0
Comment
Question by:codeoxygen
[X]
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
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40022301
Dbnull.value
0
 
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.
0
 

Author Comment

by:codeoxygen
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
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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.
0
 
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.
0
 

Author Comment

by:codeoxygen
ID: 40023053
example for declaring as nullable
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40023107
int? po
0
 
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?.
0
 
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.
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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

649 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