473,769 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing null values to a SQL Stored Procedure.

Is it possible to pass a null value to a stored procedure in .net?

I have a search Sproc that can take one of two numbers to search on,
but not both. I use the code below to pass a null value to the sproc
if a TextBox is empty.

SqlCommand getLesNumbers = new SqlCommand();
getLesNumbers.C ommandText="usp _getLesNumbers" ;
getLesNumbers.C ommandType = CommandType.Sto redProcedure;
getLesNumbers.C onnection = msoConn;

SqlParameter param;
param = getLesNumbers.P arameters.Add(" @order_No",SqlD bType.Int);
param.Direction = ParameterDirect ion.Input;
param.Value = (txtOrder_No.Te xt =="") ? null : txtOrder_No.Tex t;

This generates an error when there is nothing in the txtOrder_No box:
Procedure 'usp_GetLesNumb ers' expects parameter '@order_no', which was
not supplied

I would like to supply a null value to indicate that this field has
not been filled out. I could do this with an empty string, but I think
that null is a more explicit way to indicate that a field is not
filled out. The technique works in classic asp, so I assume there
should be a way to do it in .net

I'm using SQL7 with win2k and the latest service packs on both.

Thanks,
John
Nov 18 '05 #1
1 13631
I suspect you're looking for DBNull Class in .NET.

http://msdn.microsoft.com/library/de...classtopic.asp

"This class is used to indicate the absence of a known value, typically in a
database application.

In database applications, a null object is a valid value for a field. This
class differentiates between a null value (a null object) and an
uninitialized value (the DBNull.Value instance). For example, a table can
have records with uninitialized fields. By default, these uninitialized
fields have the DBNull value."

"John Hoge" <jh******@yahoo .com> wrote in message
news:d0******** *************** ***@posting.goo gle.com...
Is it possible to pass a null value to a stored procedure in .net?

I have a search Sproc that can take one of two numbers to search on,
but not both. I use the code below to pass a null value to the sproc
if a TextBox is empty.

SqlCommand getLesNumbers = new SqlCommand();
getLesNumbers.C ommandText="usp _getLesNumbers" ;
getLesNumbers.C ommandType = CommandType.Sto redProcedure;
getLesNumbers.C onnection = msoConn;

SqlParameter param;
param = getLesNumbers.P arameters.Add(" @order_No",SqlD bType.Int);
param.Direction = ParameterDirect ion.Input;
param.Value = (txtOrder_No.Te xt =="") ? null : txtOrder_No.Tex t;

This generates an error when there is nothing in the txtOrder_No box:
Procedure 'usp_GetLesNumb ers' expects parameter '@order_no', which was
not supplied

I would like to supply a null value to indicate that this field has
not been filled out. I could do this with an empty string, but I think
that null is a more explicit way to indicate that a field is not
filled out. The technique works in classic asp, so I assume there
should be a way to do it in .net

I'm using SQL7 with win2k and the latest service packs on both.

Thanks,
John


Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
16947
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
10
125461
by: Resant | last post by:
I have a query : Exec 'Select * From Receiving Where Code In (' + @pCode + ')' @pCode will contain more than one string parameter, eg : A1, A2, A3 How can i write that parameters, I try use : set @pCode='A1','A2','A3' but get an error : Incorrect syntax near ','
4
3001
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and pass it to the Db and retrieveing data from a stored procedure, but I can't get the hang of parameters. I have a method where I can get the parameters passed to the sp but it doesn't want to return any results. Here's a copy of my code:
9
10938
by: anilcool | last post by:
Hi all, Another novice question. I want to insert multiple records into my DB2 database using stored procedure but I do not know how many I would like to insert at any given time. Each record has 6 columns (fields). Here's an example of a stored procedure that accepts 6 parameters and inserts one record. I would like to modify this stored procedure such
1
11480
by: David Shorthouse | last post by:
Hey folks, I am attempting to pass null as the input value from a series of textboxes if the user does not input a value prior to submit. To try and do this, I am using a vbscript function on this asp as follows: CommentsAdd = IIf(Request.Form("Comments")="",NULL,Request.Form("Comments")) I was hoping this would convert those emptystring textboxes to null.
4
2760
by: Ranginald | last post by:
Hi, I'm having trouble passing a parameter from my default.aspx page to my default2.aspx page. I have values from a query in a list box and the goal is to pass the "catID" from default.aspx to a stored procedure on the details2.aspx page. I can successfully pass the values from the listbox control to a
1
14963
by: chariclark | last post by:
This may be a quick fix post... ---------------------------- I am having trouble passing multiple values into stored procedure. Here it is below: CREATE Procedure spGetAssociateds ( @PDSI int ) AS
1
2116
by: vncntj | last post by:
I have a C#.NET that simply passes 6 values to a Stored Procedure. But I'm trying to get the (Default.aspx.cs page) to handle passing the values to the sp. The goal is to pass the values and see if any records are returned. I will later insert some conditional statements. Here is my Default.aspx.cs protected void btnNext_Click(object sender, EventArgs e) {
4
5609
by: qwedster | last post by:
Howdy folks! I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries. However how to check if a value (that is null) exists in database table and return 0 if exists or else -1 using stored procedure? Please help. USE GO IF EXISTS (SELECT name FROM sys.databases WHERE name = 'ExampleDatabase')
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.