Link to home
Start Free TrialLog in
Avatar of Olukayode Oluwole
Olukayode OluwoleFlag for Canada

asked on

Reader.Hasrows Reports true but I cant get the rows read Why is This ?

I have the following method (below)  in my c# application

 
  public List<PermissionsModel> GetFormPermissions()
        {
            //List<Models.GeneralSetup.PermissionsModel> results = new List<Models.GeneralSetup.PermissionsModel>();
            List<PermissionsModel> results = new List<PermissionsModel>();
            using (var conn = new NpgsqlConnection(pgrstring))
            {
                using (var command = new NpgsqlCommand("public.sppermissions_getforrole", conn))
                {
              
                    conn.Open();
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new NpgsqlParameter("companycodex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticcompany });
                    command.Parameters.Add(new NpgsqlParameter("formnamex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticformname });
                    command.Parameters.Add(new NpgsqlParameter("rolex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Input, Value = LoginDetails.staticrole });
                    command.Parameters.Add(new NpgsqlParameter("formexecutex", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("addbuttonx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("updatebuttonx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("deletebuttonx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnusetupx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnupayablesx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnureceivablesx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnuinventoryx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnugeneralledgerx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnupersonnelx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnupayrollx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnureportsx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("mnuhelpx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    command.Parameters.Add(new NpgsqlParameter("validrecordynx", NpgsqlTypes.NpgsqlDbType.Varchar) { Direction = ParameterDirection.Output });
                    

                    PermissionsModel model = new PermissionsModel();
                    NpgsqlDataReader reader = command.ExecuteReader();
                  //  if (reader?.HasRows != null)
                    if (reader?.HasRows == true)
                    {
                        //while (reader.Read())
                        //{
                            if ((string)reader["formexecutex"] != null)
                            {
                                LoginDetails.staticformexecute = (string)reader["formexecutex"];
                                LoginDetails.staticformexecute = (string)reader[0];
                                
                                results.Add(model);
                            }
                        //}
                    }
                }
            }
            return results;
        }

Open in new window


The stored procedure which the script runs was tested with the 3 input parameters and the 14 output parameters
and the test was successful .  See screen below:

User generated image
When I executed the stored procedure withing the body of the method above

Reader.Hasrows  reported   true

But while trying to read the rows i got the error that the row is not available (see screen below)

User generated image
The method bumps out after the Reader.Hasrows

What I am doing wrong and why can't i get the data  i got from the manual query
out in my method

Thanks
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Olukayode Oluwole

ASKER

Thanks as always Saige. Some how you always come to my rescue.

I appreciate your input.

Olukay