passing null value as an argument to a function

a1jit

Regular
Joined
Aug 19, 2005
Messages
89
Hi,

Im using framework 1.1, i was just wondering how can i pass a 'null' value
to a function, because im retreving some data from database and some column might contain nulls, so how can i pass this null value to the function
for further processing

Some poeple recommend to use an "illegal" value and have
that represent null.

like use something like "-1".

I plan to avoid the need of using -1 and other characters since if the column is "-1" , it brings up another meaning.

If there any other way of passing null besides using an "illegal" value and then
converting it back to null in the receiving function?
 
Forgive me if I'm misunderstanding something, but can't you simply pass the keyword null?
C#:
// function
private void MyFunction(string val)
{
    // do something
}

// use
MyFunction(null);
 
In which case you will need to provide more information to get an informed answer. I would imagine your function is throwing an error because you are attempting todo something with the null value. When passing null values you have to ensure that a function will not error by attempting to perform operations on that null value.
 
Since you mentioned database, I assume you want to check for an actual NULL in the database - different from a null/Nothing in C#/VB.NET. Use System.DBNull.Value for your SQL parameter. If you're using a DataSet, make sure the value in the appropriate column is DBNull.Value.

If you're building a dynamic string to send to SQL you'll have to put in the string null without quotes, same as you would in a Query window. For example, if you had a Table1 with 4 columns and the int3 column needed a null value:
Code:
INSERT INTO Table1 (int1, string2, int3, date4) VALUES (1, 'hello', NULL, '1/31/1999')

-ner
 
One solution when calling Stored Procedure is to set default value to some parameters so that you don't have to explicitly send something through .NET.

The only verification is done on SQL Server side and everybody's happy and no type convertion issues.
 
Back
Top