Abol_Fa
Abol_Fa

Reputation: 112

SqlDataReader does not have the correct columns count

I'm designing a website using asp.net (webforms) and C#. I have a table which has 23 columns.

In the code below, reader is a SqlDataReader and command is a SqlCommand object. I'm using the following code to output query result from it:

command.CommandText = "SELECT * FROM devices WHERE device_level='" + ACCESS_LVL + "'" + "ORDER BY device_name";
reader = command.ExecuteReader();

and then I loop through it using

while(reader.Read());

to output the results.

However accessing some columns causes an index out of range exception (indexes above 17). My table has 23 columns but reader.FieldCount returns only 18.

This issue is not happening on my local machine when debugging the code. It only happens when I upload the project to my web server.

Using mylittleadmin database management panel (installed on the remote server) I can see all of the 23 columns.

Upvotes: 2

Views: 1232

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062590

The SQL server is going to be correct here. If SELECT * FROM devices ... returns 18 columns, then: the devices object on the database being connected to: has 18 columns. It sounds like you've let your schemas diverge between different environments / databases. Alternatively: it is possible that you have multiple logins, and login-specific objects (i.e. objects in two different database schemas in the same database), so: when you query it as you you see one object as devices - and when you query it as the application account you see a different object. Typically, this would be dbo.devices vs abolfa.devices (for example).

Upvotes: 7

Related Questions