Skip to content
Advertisement

Stuck on SqlDataReader.GetValues Method

I am making a call to a SQL stored procedure which is returning a small table of users and their bosses. What I would like to do is compare this information against a variable in a different part of my application to enable/disbale editing of a GridView row. To accomplish this, it seems that the GetValues method would return what I need, which is the entire dataset the stored procedure is returning. My thikning is that once I have that data in my app, I could load that dataset into an array, and loop through it to do what I need to do.

The problem is this. In my code below I am getting the error

Cannot implicitly convert type ‘int’ to ‘object’

When I look at the documentation on this method, the return value is an int.

My question is, since the stored procedure contains string data (user and boss names), why is the GetValues mehtod returning an int? How is my actual data represented as a number And how do I get my data into an string based array?

I’ve been looking at many examples and information on the internet, this seems to be a common problem new people have. I’m just not understanding or getting it, and I’m not making any progress. Any help is greatly appreciated!

Advertisement

Answer

The error you encounter is located in this line:

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:

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:

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):

Then retrieve the rows as follows:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement