Set message subject here ...

  • Thread starter Thread starter pjarvis
  • Start date Start date
P

pjarvis

Hi guys,

Say for example i have a hypothetical function taking 4 parameters such as the one below:

public void InsertRow(char gender, double decimalAge, double height, double weight)
{
//Adds data into a MySQL database
}

If i have a piece of data missing such as height, but i still want to call the function that will add the other parameters into the database, how would i do it? I'd like to pass
a 'null' to the function, is there any way of achieving this without getting compiler errors?

Thanks in advance

Paul
 
The general idea would be to pass either null, or System.DbNull.Value, or
whatever default value, handle such parameters inside the body of your
method, and go ahead and do your insert to your database.
Peter
 
As Peter said the actual db addition of a null would be System.DbNull.

But i am guessing you want to also know how to make your method allow null
parameter values?

if so its like this

public void InsertRow(char gender, double? decimalAge, double? height,
double? weight)
{
//Adds data into a MySQL database
}

the ? after double, double? or with an int, int? just means it can have a
null value. When inserting that null into db use the System.DbNull.
 
C# does not support optional parameters

I would make sure that you don't use NULLs anywhere
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top