T Cannon
T Cannon

Reputation: 29

DataTable Not Returning Data

What is my method missing? It is not returning the data table.

public DataTable GetHotelReportData(int _ratpropid) {

            var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;

            DataTable dt = new DataTable();
            dt.Clear();
            SqlConnection conn = new SqlConnection(_connectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@RatPropId", _ratpropid);
            SqlDataAdapter da = new SqlDataAdapter();

            try {
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception _ex) {
                new ErrorLogging().Log(_ex);
            }
            finally {
                conn.Close();
                da.Dispose();
                cmd.Dispose();
            }
            return dt;
        }

Upvotes: 0

Views: 181

Answers (1)

Sturgus
Sturgus

Reputation: 686

Try cutting the data adapter out and just using the command. No need for an adapter as far as I can tell:

public DataTable GetHotelReportData(int _ratpropid) {

        var _connectionString = _isDevelopment ? CommonTypes.Dev : CommonTypes.Prod;

        DataTable dt = new DataTable();
        dt.Clear();
        SqlConnection conn = new SqlConnection(_connectionString);
        SqlCommand cmd = new SqlCommand("ww.HotelRpt_spGenerateData2018", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RatPropId", _ratpropid);


        try {
            conn.Open();
            dt.Load(cmd.ExecuteReader);
            return dt;
        }
        catch (Exception _ex) {
            new ErrorLogging().Log(_ex);
        }
        finally {
            conn.Close();
            cmd.Dispose();
        }

    }

If that doesn't work, try executing your stored procedure natively in SSMS (or another DBMS) to see if you get any results there.

Upvotes: 1

Related Questions