Solved

.NET oledbcommand update retain values if some parameters are null

Posted on 2004-03-30
4
1,004 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual xHarbour 1 74
copyEvens challenge 6 63
noX challenge 17 75
VB6 Compile Compatibility Issue 4 23
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

11 Experts available now in Live!

Get 1:1 Help Now