Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2004, 07:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default WebService question

In one of the web methods I have within my web service I have the following code:

public void DeleteRecords( int intRecordID )
{
//Make sure the user is authenticated before running this WEB METHOD.
if (securityTicket.IsTicketValid(sessnID) == true)
{
    return "Authenticated";
}
else
{
    return "Failed To Authenticate";
}

// Get a connection to the database.
SqlConnection dbConnection = new SqlConnection();
dbConnection = XXX.UUU.Database.GetDBConnection();

// Create a command object.
SqlCommand dbCommand = new SqlCommand();

// Set up the properties for the command and connection objects.
dbCommand.Connection = dbConnection;
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.CommandText = "Delete_Records";

// Variable Name (Input)
dbCommand.Parameters.Add("@RecordID", SqlDbType.Int);
dbCommand.Parameters["@RecordID"].Direction = ParameterDirection.Input;
dbCommand.Parameters["@RecordID"].Value = aintRecordid;

// Execute the stored procedure.
dbCommand.ExecuteNonQuery();

// Close the connection to the database.
dbCommand.Connection.Close();
}

How do I add a try catch and throw to this code since the method returns a void? Is there some other way that is better to handle this type of code? I am trying to authenticate the user before they run this WebMethod.

Any suggestions or help would be appreciated. Thanks.
 
Old April 26th, 2004, 07:59 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

There may be better ways, but one way I have used a couple of times is to return either a string or a DataSet from your method, even if it's a void type of method.

That way, the client can examine the DataSet or string, figure out what is going on and act accordingly....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Brother Wolf; Sister Moon by The Cult (Track 5 from the album: Love) What's This?

 
Old April 26th, 2004, 08:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,

Thanks much. I'll try that. I just assumed that because this contains a stored proc. to only delete records and not report anything back then I thought we could only use VOID.

Thanks again. I'll reply back if I run into a problem.
 
Old April 26th, 2004, 08:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

True. In other circumstances, that would be good enough (although even then you could have a return type of bool to indicate success or failure).

However, you can't really throw a useful error, so you need to return something that contains error information. The good thing about a DataSet is that you can return detailed, structured info. You could, for example, return a ErrorDescription, Number, Severity, a HelpURL with more info etc. Other .NET solutions can read and use the DataSet as a DataSet, other platforms can access it as a normal XML stream.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Bleeding heart by Jimi Hendrix (Track 10 from the album: Purple Haze) What's This?

 
Old April 26th, 2004, 09:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default

Having this code:

if (securityTicket.IsTicketValid(sessnID) == true)
{
    return "Authenticated";
}
else
{
    return "Failed To Authenticate";
}

in my web method denotes now this error:

Cannot implicitly convert type 'string' to 'System.Data.DataSet'

I did not see a CONVERT option. Any suggestions or help would again be appreciated.

Thank you.
 
Old April 26th, 2004, 10:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, that makes sense. When your method returns a DataSet, it should return a DataSet not a string. Also, you can't expect a string to be converted to a DataSet, as a DataSet contains meta data about itself. Try something like this:
Code:
if (securityTicket.IsTicketValid(sessnID) == true)
{
  // Construct DataSet with useful return stuff here
}
else
{
  DataTable MyErrorsTable = new DataTable("ReturnedErrors");
  MyErrorsTable.Columns.Add("ErrorMessage",System.Type.GetType("System.String"));
  MyErrorsTable.Columns.Add("ErrorNumber",System.Type.GetType("System.Int32"));
  DataRow MyDR = MyErrorsTable.NewRow();
  MyDR("ErrorMessage") = "Your Error Message Here";
  MyDR("ErrorNumber") = -100;
  MyErrorsTable.Rows.Add(MyDR);
  DataSet ErrorsDataSet = new DataSet("Errors");
  ErrorsDataSet.Tables.Add(MyErrorsTable);
  return ErrorsDataSet;
}
I am typing this right into P2P, so it may not compile at once. For a more detailed explanation of all this, look here: http://www.devx.com/dotnet/Article/20369/1954?pf=true

Alternatively, set up your method to return a string:
Code:
public string DeleteRecords( int intRecordID )
{
This way, the code you posted in your last message will work fine. However, the disadvantage is that you can only return a simple string, and no detailed information about the error.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Give It Revolution by Suicidal Tendencies (Track 5 from the album: Lights, Camera, Revolution) What's This?
 
Old April 27th, 2004, 08:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,

Again, thanks for your time and all your help. It is appreciated. I am building the try, catch statement.






Similar Threads
Thread Thread Starter Forum Replies Last Post
to know Webservice vingo_mail ASP.NET 2.0 Basics 1 March 3rd, 2008 01:16 PM
how to pass a value in this webservice? hertendreef ASP.NET 2.0 Professional 0 December 24th, 2006 12:19 PM
Help me about WebService hueduongit .NET Web Services 2 June 3rd, 2004 09:51 PM
webservice hanuyedluri .NET Web Services 1 March 21st, 2004 05:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.