473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Parameterized Query, UPDATE statement, and Nulls

I am writing a parametrized query, where Im building an UPDATE
statement through concatenation, and then adding the parameters such
as:

UPDATE myTable SET [columna] = @newcolumna, [columnb] = @newcolumnb
WHERE [columna] = @origcolumna
AND [columnb] = @origcolumnb

This works fine, unless one of the original database values are a
NULL. Im creating the parameters as:

SqlParameter sqlOrgParam = new SqlParameter();
sqlOrgParam.SqlDbType = this.GetColumnType(tableName,
rowUpdate.ColumnName); // my own method for type
sqlOrgParam.ParameterName = "@org_" + rowUpdate.ColumnName;
if (rowUpdate.OriginalValue == "") // By now, an empty
string would represent a null
sqlOrgParam.Value = DBNull.Value; //This line executes as it
should
else
sqlOrgParam.Value = rowUpdate.OriginalValue;
sqlCmd.Parameters.Add(sqlOrgParam);

The query is called without failure, its just that it doesn't find the
null in the original record, and as such, doesn't perform any
updates. If I remove the null in the table, everything works fine.
It seems like parameter substitution doesnt like DBNull.Value for some
reason. Any thoughts?
Jul 6 '08 #1
3 3109
This is an SQL issue not a .Net issue.

A column with a null value does NOT = a parameter with a null value -
it 'Is Null'.

So - your SQL needs to be something like

WHERE (columna is null and @origValue is null)
OR )columna = @origValue
...

On Jul 7, 9:19*am, xlar54 <scott.hut...@gmail.comwrote:
I am writing a parametrized query, where Im building an UPDATE
statement through concatenation, and then adding the parameters such
as:

UPDATE myTable SET [columna] = @newcolumna, [columnb] = @newcolumnb
WHERE [columna] = @origcolumna
AND [columnb] = @origcolumnb

This works fine, unless one of the original database values are a
NULL. Im creating the parameters as:

SqlParameter sqlOrgParam = new SqlParameter();
sqlOrgParam.SqlDbType = this.GetColumnType(tableName,
rowUpdate.ColumnName); *// my own method for type
sqlOrgParam.ParameterName = "@org_" + rowUpdate.ColumnName;
if (rowUpdate.OriginalValue == "") * * * * * * *// By now, an empty
string would represent a null
* * * *sqlOrgParam.Value = DBNull.Value; * //This line executes as it
should
else
* * * *sqlOrgParam.Value = rowUpdate.OriginalValue;
sqlCmd.Parameters.Add(sqlOrgParam);

The query is called without failure, its just that it doesn't find the
null in the original record, and as such, doesn't perform any
updates. *If I remove the null in the table, everything works fine.
It seems like parameter substitution doesnt like DBNull.Value for some
reason. *Any thoughts?
Jul 7 '08 #2
On Jul 6, 7:00*pm, ".\\\\axxx" <mailma...@gmail.comwrote:
This is an SQL issue not a .Net issue.

A column with a null value does NOT = a parameter with a null value -
it 'Is Null'.

So - your SQL needs to be something like

WHERE (columna is null and @origValue is null)
OR )columna = @origValue
...
Perhaps you are not understanding the issue. I can not say "is null"
in a .NET C# parametrized query.

Thanks
Jul 7 '08 #3
I can not say "is null" in a .NET C# parametrized query.
Why not? The query is just TSQL; you can say whatever you want...
under ANSI SQL nulls are never equal, so you have to do something...

the "([foo] IS NULL AND [bar] IS NULL) OR ([foo] = [bar])" is quite a
common bit of SQL (replace [foo] and [bar]...) - alternatively you can
build the SQL based on the values (via a StringBuilder etc) and append
only the correct construction. Or sometimes you can get creative with
ISNULL/NULLIF/COALESCE - but this isn't always a good idea ;-p

Of course, another option would be to use a timestamp for concurrency
checking, or any of the ORM tools - LINQ-to-SQL will do either
timestamp or column-based concurrency checking.

Marc
Jul 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
4
by: Gilberto Campos | last post by:
Hi all. I am having a strange problem. I am developping an application that acceses an Access db through Jet (.UDL files). I have writen parametric INSERT queries that work fine. I am now...
12
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...
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
4
by: mcca0081 | last post by:
hi - i'm trying to populate the field rateintegrated with an update sql statement. it is a calculated value from two other fields, feeloc + feeremarketing. everytime i type in a value for feeloc,...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
5
by: bobh | last post by:
Hi All, I have this query which updates a field based on the result of an IIF statement. The table is on SQLServer and I'm linked to it and it has many records and will take Access a very long...
5
by: Chris Kennedy | last post by:
Hi all I'm running SQL Server 2005 Express Edition. One database One table called Sites Fields as follows: id (bigint, Identity, Primary Key) SiteName (varchar(50), allows nulls) Generation...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.