We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

.NET oledbcommand update retain values if some parameters are null

ee_id
ee_id asked
on
Medium Priority
1,116 Views
Last Modified: 2008-03-17
Hello,

I have a question about the oledbcommand object in .NET.  I am using it to update a record, however, not all of the fields of the record will need to be changed if the updating values are null.  How can I retain the value in the record for those parameters that are actually null.  SQL provides this by referencing the field name itself in brackets (e.g. [fieldname]).  I tried this, and I tried changing the OleDbType to variant but the fields just get "[fieldname]" updated.

How do you easily retain the values of the record when some of the parameters are null, and you would rather keep the values in the table than update them with null?
Comment
Watch Question

Author

Commented:
Here is my sample code:

OleDbCommand updateCMD = new OleDbCommand(@"UPDATE mdfProperties SET [11_MasterImage] = ?"

if paramter value is null then use this as the parameter value
updateCMD.Parameters.Add("p_masterImage", OleDbType.Char, 50).Value = "[11_masterImage]"  ;

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:

Thanks for your comments... to zbowling, this is just a code snippet that does work, but ofcourse it is missing non critial elements to quickly illustrate my situation.  I was trying to avoid the manual labor of using if statements to generate the SQL statement and parameters because the solution should have been as easy as exactly what I was attempting (new data type enum perhaps).  I guess my expectations of parameters are backwards, but certainly not my understanding of them.  Thanks for your feedback...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.