Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / December 2004

Tip: Looking for answers? Try searching our database.

Server-Side Data Validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mabster - 20 Dec 2004 03:25 GMT
Hi gang,

I have a web service class I'm developing along the same lines as that
described here in MSDN:

http://tinyurl.com/63sl4

http://msdn.microsoft.com/library/en-us/vsintro7/html/vbwlkcreatingdistributedwe
bapplicationwalkthrough.asp


That is, it has a GetXXX method that returns a dataset, and an UpdateXXX
method that takes a dataset and applies it to the database.

Works fine so far. I'm very happy.

However, one thing I'd like to do is some validation of individual
fields at the server side before the entire dataset is posted back.

For example, if my web service was to manage purchase orders, it would
be nice to ask my web service if a particular product was available to
order before I hit OK on my 'new order' form.

I guess my first instinct was, "Why not just pull down all the valid
products as another dataset and validate the entered product# against
them?"

That works fine for small datasets, but what if there are a million
active products? I'd much prefer to write an IsValidProduct webmethod
and be able to call that just after the user enters the product number.

How are others implementing validation like this? Should the method be:

[WebMethod]
public bool IsValidProduct(int prod)
{
   // return true if valid, false if not
}

or

[WebMethod]
public bool IsValidProduct(int prod, out string Error)
{
   // return true if valid, false if not, and populate Error
   // with meaningful string
}

or

[WebMethod]
public void ValidateProduct(int prod)
{
   // if product is not valid, raise exception
}

Any pointers?

Thanks,
Matt
Mujtaba Syed - 20 Dec 2004 03:40 GMT
Hi Matt:

> [WebMethod]
> public bool IsValidProduct(int prod)
> {
>    // return true if valid, false if not
> }

This is good (if you don't want to pass a status message back).

> [WebMethod]
> public bool IsValidProduct(int prod, out string Error)
> {
>    // return true if valid, false if not, and populate Error
>    // with meaningful string
> }

Hmm... You won't be able to consume this web method from a VB.NET client
(you will have to use an initialized ByRef parameter). Also, you will need
to specify the SoapRpcMethod attribute with this web method, making it
non-conformant with WS-I Basic Profile 1.1

A better way is:

class Status
{
   public bool isValid;
   public string message;
}

public Status IsValidProduct (int productId)
{
   //...
}

> [WebMethod]
> public void ValidateProduct(int prod)
> {
>    // if product is not valid, raise exception
> }

Don't use exceptions for program flow.

Hope this helps,
Mujtaba.
mabster - 20 Dec 2004 03:45 GMT
> Hi Matt:
>
>>public bool IsValidProduct(int prod)
>
> This is good (if you don't want to pass a status message back).

Yeah, but it's pretty useless to the end-user if I don't. Next ...

>>public bool IsValidProduct(int prod, out string Error)
>
> Hmm... You won't be able to consume this web method from a VB.NET client
> (you will have to use an initialized ByRef parameter). Also, you will need
> to specify the SoapRpcMethod attribute with this web method, making it
> non-conformant with WS-I Basic Profile 1.1

Sounds like work :) Next ...

> A better way is:
>
[quoted text clipped - 5 lines]
>
> public Status IsValidProduct (int productId)

Ok, that sounds reasonable. Does the FCL define a type similar to your
'Status' example above for this sort of thing? It sounds like a common
enough task.

>>[WebMethod]
>>public void ValidateProduct(int prod)
[quoted text clipped - 3 lines]
>
> Don't use exceptions for program flow.

Yeah, I didn't like that last one either.

That said - it makes sense to raise an exception in the UpdateXXX method
if data is invalid, right? I mean, the UpdateXXX method *expects* valid
data - anything else has to be an exception.

Thanks for you reply!

Matt

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.