The error you encounter is located in this line:
myObject = reader.GetValues(myObject);
myObject
is an array of objects and the GetValues
method returns an integer that contains the count that is placed in the array. So in order to solve your error, you can change the line to the following:
var count = reader.GetValues(myObject);
Also, in your code, you only retrieve one row of data. This is - of course - fine if you only expect one row. When expecting multiple rows, you typically loop over the rows like this:
while (reader.Read())
{
// Read row and add row to list
}
For a sample on how to use SqlDataReader, see this link.
Sample
If you want to retrieve multiple rows and store the data, I suggest to add a class to store the data of a row (you might want to verify the data types so that they match the types that the SP returns):
public class Data
{
public string DeptApp { get; set; }
public string Status { get; set; } // this might be another data type
public string Supervisor { get; set; }
}
Then retrieve the rows as follows:
public Data[] GetDeptAppData()
{
//execute stored procedure to get data from database
using (SqlConnection sqlConnection = new SqlConnection("Data Source=MyServer;Initial Catalog=MyCatalog;Persist Security Info=True;User ID=MyUser;Password=MyPassword"))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SP_Admin_DeptApp_Load";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = sqlConnection;
sqlConnection.Open();
using (var reader = cmd.ExecuteReader())
{
if (!reader.HasRows)
{
// throw exception
}
var lst = new List<Data>();
while (reader.Read())
{
var row = new Data();
row.DeptApp = reader.GetString(0);
row.Status = reader.GetString(1);
row.Supervisor = reader.GetString(2);
lst.Add(row);
}
return lst.ToArray();
}
}
}