简体   繁体   中英

how to create class that return datatable in c#?

i created a class that return a dataTable but it show an error

Error 1 'demo_C_Sharp.Cls_onnection.GetApprovedData(ref string)': not all code paths return a value D:\\Vivek\\demoproject\\demo C Sharp\\demo C Sharp\\class files\\Cls_onnection.cs 45 26 demo C Sharp

i created following method :

 public  DataTable  GetApprovedData(ref string tablename)  
      {
             try 
             {          
             if (cnn.State==System.Data.ConnectionState.Closed)
                {
                    dtTbl = new DataTable();
                    cnn.Open();
                    cmd = new  SqlCommand("USP_Billing_GetApprovedData", cnn);
                    cmd.CommandTimeout = 5000;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ProcessName", tablename);

                    da = new SqlDataAdapter(cmd);
                    da.Fill(dtTbl);
                    return dtTbl;
                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message);
            }    

All parts must return a value.See here

You return only in the if part, and what if the if part will not work ? So you need also en extra variant which may be at the end of the method.

public  DataTable  GetApprovedData(ref string tablename)  
{
   dtTbl = new DataTable();

   try 
   {            
       if (cnn.State==System.Data.ConnectionState.Closed)
       {
           cnn.Open();
           cmd = new  SqlCommand("USP_Billing_GetApprovedData", cnn);
           cmd.CommandTimeout = 5000;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@ProcessName", tablename);

           da = new SqlDataAdapter(cmd);
           da.Fill(dtTbl);

        }
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message);
    }   
    finally
    {
       cnn.Close();
    }

    return dtTbl;
}

You must return something from the method, or throw an exception. Currently in your catch block you're showing a message, but then doing nothing else. You're swallowing the exception, so the code expects to be able to continue, but then you don't tell it what to return from the method, which is not allowed.

There's also a similar problem in that the return statement is also within an "if" block. So when the conditions of the "if" are not satisfied, where does the code go? Nowhere, which means it doesn't know what to return there either.

I suggest you move the declaration and return statements for dtb1 outside the try block. Alternatively, within the catch block return an empty DataTable object, or even null if that's appropriate for your situation, and the same in an extra else after your if .

Your return path is in the try block, which may fail at any point and not reach the return statement.

You'll need to make sure you always hit a return statement, even if you catch an exception.

Your problem is not in the class as you name the post, but in method. Anyway, this is not of a big issue here.

Moreover, as an error hints you, you have to always return something (should it be null or empty instance of DataTable ).

Here it complains, because in case that connection is not closed, the method doesn´t return anything at all. And that is the problem.

Your solution should be such that you should add a code to handle the rest of cases for the if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM