Answered by:
web service error handling

Question
-
User-1904303448 posted
Hi all,
I have this WebMethod
[WebMethod] public DataTable data(string test) { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("sp", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@test", test); try { con.Open(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataTable ds = new DataTable("dt"); try { da.Fill(ds); } catch (SqlException ex) { throw ex; } return ds; } catch (Exception ex) { throw ex; } finally { con.Close(); } }
If I pass as parameter a value that isn't correctly formatted I have an SQLException, but I don't find a way to manage it.
Another question: is there a way to return EVER a <error></error>, empty if there aren't errors, and filled with the error if it occours?
Thanks in advance
Monday, March 25, 2013 7:28 AM
Answers
-
User551462331 posted
try this approach
http://www.agile-code.com/blog/asp-net-web-service-returning-properly-formatted-error-message/
hope this helps...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 25, 2013 7:37 AM -
User-1662538993 posted
Try this way -
DataTable table = new DataTable(); table.Columns.Add("Error", typeof(string)); // // Here we add five DataRows. // table.Rows.Add("Error Message"); return table;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 25, 2013 10:43 AM
All replies
-
User551462331 posted
try this approach
http://www.agile-code.com/blog/asp-net-web-service-returning-properly-formatted-error-message/
hope this helps...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 25, 2013 7:37 AM -
User-1904303448 posted
Thanks
but if I implement this way:
[WebMethod] public SomeResultObject SomeWebMethod() { var returnObject = new SomeReturnObject(); try { /* code here */ } catch (Exception ex) { /* Some logging logic here...*/ //we are not re-throwing an error, but setting //the error message to the return object. returnObject.ContainsErrors = true; returnObject.ErrorMessage = ex.Message; } return returnObject; }
how can I fill the datatable with these info?
Monday, March 25, 2013 7:45 AM -
User-1662538993 posted
In your catch blog create one datatable insert a row with error message and return that datatable.
Monday, March 25, 2013 8:47 AM -
User-1904303448 posted
catch (Exception ex) { DataRow errorRow = dt.NewRow(); errorRow["error"] = ex.Message.ToString(); dt.Rows.Add(errorRow); return dt; }
error: "the column 'error' is not on dt" (translated from italian)
Monday, March 25, 2013 9:20 AM -
User-1662538993 posted
Try this way -
DataTable table = new DataTable(); table.Columns.Add("Error", typeof(string)); // // Here we add five DataRows. // table.Rows.Add("Error Message"); return table;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, March 25, 2013 10:43 AM