Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / January 2006

Tip: Looking for answers? Try searching our database.

Simple problem passing nulls with ADO.net 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Harvey - 15 Jan 2006 17:20 GMT
Hi everyone,

I'm having a really simple problem - I can't seem to insert a null value
into the database. When I do it tells me that the procedure expects the parameter
and that I'm not providing it.
Well, I am, its just a null value!

The code is:

    con.Open();
    trans = con.BeginTransaction();
   
            cmd = new SqlCommand("UpdateProperty", con, trans);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@id", id));
    cmd.Parameters.Add(new SqlParameter("@propertyName", propertyName));
            cmd.Parameters.Add(new SqlParameter("@city", null));        
                                    // THE PROBLEM

            cmd.ExecuteNonQuery();

When executing the command, it throws an exception that says that the procedure
expects the @city parameter.

Can anyone tell me how to send the database a null value! The column definition
in the database is more than happy to take nulls

Thanks to anyone who can help

Kindest Regards

Simo
Peter Bromberg [C# MVP] - 15 Jan 2006 17:49 GMT
Simon,
The C# "null" keyword is not the same as System.DbNull.Value. You might try
that. It still depends on how the database is set up, and also,how the stored
proc is written.
Peter

Signature

Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

> Hi everyone,
>
[quoted text clipped - 29 lines]
>
> Simon
William (Bill) Vaughn - 15 Jan 2006 19:45 GMT
Try passing DbNull.Value.
null in C# means "don't set the Parameter value" so SQL Server assumes you
mean to take the Parameter default.

hth

Signature

____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

> Hi everyone,
>
[quoted text clipped - 29 lines]
>
> Simon
Otis Mukinfus - 15 Jan 2006 22:39 GMT
>Hi everyone,
>
>I'm having a really simple problem - I can't seem to insert a null value
>into the database. When I do it tells me that the procedure expects the parameter
>and that I'm not providing it.
>Well, I am, its just a null value!

Peter and Bill are both correct, but another solution is to give the
parameter in the stored procedure the default value of NULL thus:

@YourParam int = NULL

Then The procedure will assign the vale of null to the parameter if it
is not included in the input.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Simon Harvey - 16 Jan 2006 19:45 GMT
Many thanks to you all for your help

:-)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.