1

I translated this part of the code from vb to c# and giving me this error message. "Not all code paths return a value". What is the problem? Thanks in advance.

 public DataSet LoadSearchDataSet(string strConnection, string strSQL) { //The purpose of this function is to create and populate a data //set based on a SQL statement passed in to the function. try { DataSet dsData = new DataSet(); //call the table in the local dataset "results" since the values //may be coming from multiple tables. string strTableName = "Results"; bool blnRunStoredProc = false; dsData = PopulateDataSetTable(strConnection, strTableName, strSQL, blnRunStoredProc, dsData); WriteSampleDataToOutputWindow(dsData); //return the data set to the calling procedure return dsData; } catch { //error handling goes here UnhandledExceptionHandler(); } } 
3
  • 4
    The problem is exactly as the compiler says - not all code paths return a value. What do you expect to happen if you catch an exception? You're calling UnhandledExceptionHandler, but then what do you expect to be returned? Commented May 26, 2015 at 6:55
  • Your catch statement must also return a value! Or place the return value outside the try catch Commented May 26, 2015 at 6:55
  • You need a throw (or a return of some kind) in that catch Commented May 26, 2015 at 6:56

8 Answers 8

4

You are missing the return value in the case the code throws an exception.

public DataSet LoadSearchDataSet(string strConnection, string strSQL) { //The purpose of this function is to create and populate a data //set based on a SQL statement passed in to the function. DataSet dsData = new DataSet(); try { //call the table in the local dataset "results" since the values //may be coming from multiple tables. string strTableName = "Results"; bool blnRunStoredProc = false; dsData = PopulateDataSetTable(strConnection, strTableName, strSQL, blnRunStoredProc, dsData); WriteSampleDataToOutputWindow(dsData); } catch { //error handling goes here UnhandledExceptionHandler(); } //return the data set to the calling procedure return dsData; } 
Sign up to request clarification or add additional context in comments.

Comments

2

If an exception occurs in your try block before the return statement, the catch is executed and that does not return anything, because you did not tell it to.

You can do one of these:

  • Return a value from the catch block. Do this only if it makes sense and you have a sensible value you can return. Be aware that returning null is a usual source of bugs and there are patterns out there to avoid just that.
  • Re-throw the exception that occurred, if you cannot do anything at this point about it (and return an object that makes sense). You can do this by adding a line that says: throw;
  • Throw a different error - You can package the original exception in a new one, providing extra details about the context, if necessary.

Comments

2

You need to add a return statement after your catch clause!

In case of an exception inside your try catch clause, you won't return a value. And that's exactly what your error is indicating.

Comments

0

This is a common error message in functions, as functions are designed to return some value. If your code passes the catch section, it will reach the end of the function without returning anything, thats where you need to return the value.

Comments

0

rewrite like this:

 DataSet dsData = null; try { //call the table in the local dataset "results" since the values //may be coming from multiple tables. string strTableName = "Results"; bool blnRunStoredProc = false; dsData = PopulateDataSetTable(strConnection, strTableName, strSQL, blnRunStoredProc, dsData); WriteSampleDataToOutputWindow(dsData); } catch { //error handling goes here UnhandledExceptionHandler(); } //return the data set to the calling procedure return dsData; 

Comments

0

You can resolve this issue by :

  1. change the function return to VOID (if you does not return something)
  2. give return keyword with variable name before end function

Comments

0

This is because in the case of any exception occurs,the exception will thrown to the catch, in that case the code will not return any value. so you have to return some value from the catch to avoid this issue

Replace the catch with this:

 catch { //error handling goes here UnhandledExceptionHandler(); return new DataSet(); } 

2 Comments

you have syntax error in code. Return should be return
That's not always the best thing to do. It's worth mentioning that there are some other alternatives as well.
0

It is must to return proper value in any case. so try to maintain try catch block with return value or outside of try/catch block if nothing to return in try / catch block.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.