Solved

.NET oledbcommand update retain values if some parameters are null

Posted on 2004-03-30
4
1,020 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?
0
Comment
Question by:ee_id
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:ee_id
ID: 10717402
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]"  ;

0
 
LVL 3

Accepted Solution

by:
zbowling earned 200 total points
ID: 10718958
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.
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 50 total points
ID: 10719397
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.

Dabas
0
 

Author Comment

by:ee_id
ID: 10720343

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...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn different types of Android Layout and some basics of an Android App.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Introduction to Processes

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question