Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Default SqlParameter Value To Null
Message
De
14/02/2009 12:58:47
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
14/02/2009 11:07:35
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01381129
Message ID:
01381788
Vues:
21
Bonnie,
I am surprised at your reply. I know that I have used NULL values as meaningful values before. Unfortunately I have been thinking for a half hour of an example but can't. :-(

Basically, on an INSERT I would default the value to NULL and not pass it in, like we've mentioned. But in an UPDATE sometime I would have valid reasons to pass in System.DbNull.Value.

BTW, C# rocks. I am really digging it. For some reason I keep getting caught up on lines like these though:
StringBuilder x = New StringBuilder; //Stupid lowercase new and () after empty constructor calls
Oh, and comments. It's hard to break the 'Comment habit.

>As Mike and Naomi both mentioned, your Stored Proc parameters should be defaulting to NULL and should know how to process NULL values, something like this:
>
>
>CREATE PROCEDURE bsp_ActivityPut
>	@activitykey		bigint = NULL OUTPUT,
>	@userkey		bigint = NULL,
>	@startdatetime		datetime = NULL,
>	@enddatetime		datetime = NULL
>AS
>		UPDATE Activity
>		SET userkey = ISNULL(@userkey, userkey),
>		    startdatetime = ISNULL(@startdatetime, startdatetime),
>		    enddatetime = ISNULL(@enddatetime, enddatetime),
>		WHERE activitykey = @activitykey
>
>
>
>Your C# DataAccess should check if the field is DBNull.Value and if so should not create a parameter. IOW, you never pass NULLs back to the Stored Procs.
>
>When I've said this to some people, they've countered with ... "but what if I want to reset the database column back to a NULL"? And I reply "NULL means nothing was ever entered. If a user has actually put some value in a field, saved it, and then later changed their mind, you should NOT put a NULL back into that column. It needs to default to something else that means nothing, but not a NULL".
>
>~~Bonnie
>
>
>
>
>>I have the following piece of code:
>>
>>
>>SqlParameter PartDesc = new SqlParameter();
>>PartDesc.SqlDbType = SqlDbType.VarChar;
>>PartDesc.ParameterName = "@Part_Description";
>>PartDesc.Value = txtPartDesc.Text;
>>
>>
>>If the user didn't type anything into the txtPartDesc field, how would ensure that NULL was passed to the stored procedure?
Very fitting: http://xkcd.com/386/
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform