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
Simon 2 1591
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
--
Co-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
"Simon Harvey" wrote: 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
Simon
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
--
____________________________________
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.
__________________________________
"Simon Harvey" <no******@hotmail.com> wrote in message
news:94*************************@news.microsoft.co m... 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
Simon
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Resant |
last post by:
I have a query :
Exec 'Select * From Receiving Where Code In (' + @pCode + ')'
@pCode will contain more than one string parameter, eg : A1, A2, A3
How can i write that parameters, I try use :...
|
by: strict9 |
last post by:
Hello all,
I'm writing several queries which need to do various string formating,
including changing a phone number from (123) 456-7890. After some
problem with data mismatches, I finally got it...
|
by: Norvin Laudon |
last post by:
Hi,
Should be simple, but I can't seem to get a handle on it.
I've over-ridden the == operator in my class.
public static bool operator==(XrayImage lhs, XrayImage rhs)
{
return (...
|
by: James R |
last post by:
Ok, I'm new at this and learning as I go, but under extreme pressure
from work.. So many bugs and little tricky obsticals have got me very
far behind, and this is the latest..
I made a simple...
|
by: dew |
last post by:
I am using sql to modify data, using parameters. However, if I set the
datatime to any other than a varchar, it bombs if the user enters a null
value. How am I supposed to handle this. Set...
| |
by: academic |
last post by:
When I declare a reference variable I initialize it to Nothing.
Now I'm wondering if that best for String variables - is "" better?
With Nothing I assume no memory is set aside nor GC'ed
But...
|
by: Paul M |
last post by:
Hi All,
I'm currently writing a z/OS DB2 Stored Proc in C, using an example from the
IBM Stored Procedure guide (SG24-7083-00). The database calls to read and
update the database work...
|
by: Ike |
last post by:
If I have a simple database with two tables, where tableB.crossid equals the
tableA.id, what kind of select statement would I create to select all
records in tableA that do not have their id as the...
|
by: Emin |
last post by:
Dear Experts,
I have a fairly simple query in which adding a where clause slows
things down by at least a factor of 100. The following is the slow
version of the query
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |