Solved

.NET oledbcommand update retain values if some parameters are null

Posted on 2004-03-30
4
1,005 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now