• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1048
  • Last Modified:

.NET oledbcommand update retain values if some parameters are null


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?
  • 2
2 Solutions
ee_idAuthor 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]"  ;

First of all you have the whole idea of parameters backwards. I'm not sure how that would ever work in that way.

Basicly what parameters do is basicly call a find and replace on your sql statement (unless you are calling a stored proc), but its a lot more technical then a find and replace. Parameters handle type values, phrase the type from native .NET type to the format required by sql (so if you pass a string to it, you don't have to escape single quotes and stuff because it will do it for you), and also figure out any types of errors that might occur and try to fix them.

There is no what I can really help you because there is no logical way to make what you work, work. I can give an example of how one works. I'm guess when you say null in .NET you mean the default value since nulls really don't exist unless you set them to nothing implictly which isn't common pratice in .NET. If you are talking about seting things to null in SQL unless the value you are passing is null also then you have to etheir write a really big sql statement that checks for it or use an IF statement to check.

you might be able to pull this off (might not work because I've been doing java and MySQL last few days so my mind is still in that mode)

OleDbCommand updateCMD = new OleDbCommand(@"UPDATE contacts WHERE ([LastName] == @LastName) SET [FirstName] = @FirstName AND [PhoneNumber]= @PhoneNumber"

string pLastName = "Smith";
string pPhoneNumber, pFirstName;
updateCMD.Paramaters.Add("@LastName", OleDbType.String, 255).Value = pLastName;
if(pFirstName != Null.Value || pFirstName != "") updateCMD.Paramaters.Add("@LastName", OleDbType.String, 255).Value = pFirstName;
if(pPhoneNumber != Null.Value || pPhoneNumber != "") updateCMD.Paramaters.Add("@PhoneNumber", OleDbType.String, 255).Value = pPhoneNumber;

Play with that and you can even write some code up that could show you want the sql statement looks like after you finish. Unfortunattly Parameters are one of the most undocumented functions in .NET so its hard to find help.
Hi ee_id,
> OleDbCommand updateCMD = new OleDbCommand("UPDATE mdfProperties SET [11_MasterImage] = ?"

I have the feeling that this command will update ALL records. I do not think this is what you want.
A where statement is needed too.

Alternatively you might be better off using DataAdaptors and DataSets and let .NET do the difficult work for you.

ee_idAuthor 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...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now