-2
    public DataTable SelectData(string stored_procedure, SqlParameter[] param)
    {

        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        if (param != null)
        {
            for (int i = 0; i < param.Length; i++)
            {
                sqlcmd.Parameters.Add(param[i]);
                SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
        }
    }
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • 3
    What happens when `param == null`??? Why do you have a `for` loop if you just return after the first iteration? – Ron Beyer Dec 23 '19 at 21:59

1 Answers1

1

As the error message says, you are only returning a value when your conditional is true. If it is false, you are not returning any value (and it is required by the method).

To get it to work your code should look like this:

public DataTable SelectData(string stored_procedure, SqlParameter[] param)
    {

        SqlCommand sqlcmd = new SqlCommand();
        sqlcmd.CommandType = CommandType.StoredProcedure;
        sqlcmd.CommandText = stored_procedure;
        if (param != null)
        {
            for (int i = 0; i < param.Length; i++)
            {
                sqlcmd.Parameters.Add(param[i]);
                SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
        }
        return null; // Or any value you want to return here if the condition is false
    }

Just remember to check if the result is null after calling the method to avoid any exception.

cesarmart
  • 759
  • 1
  • 7
  • 10