Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

Forgive me if I'm misunderstanding something, but can't you simply pass the keyword null?

// function
private void MyFunction(string val)
{
   // do something
}

// use
MyFunction(null);

Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
  • *Experts*
Posted

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:

INSERT INTO Table1 (int1, string2, int3, date4) VALUES (1, 'hello', NULL, '1/31/1999')

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

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.

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...