0

I have created a Class in C#, and my code is showing an error. Error is =>Cannot implicitly convert type 'System.Data.SqlClient.SqlDataReader' to 'System.Data.OracleClient.OracleDataReader'

I am getting error at return SqlHelper.ExecuteReader(CnSettings.cnString1, CommandType.Text, commandText);

public OracleDataReader getWeightmntInfoForCenter(string startdate, string enddate, string unitcode)
{
   string commandText = string.Concat(new string[]
   {
      "select m.unitcode unit_code, to_char(tran_date, 'dd-MON-yy') m_date,
      to_char(m_purchy) m_number, net AS n_weight, to_number(substr(g.ryot_code,5))
      m_grow, to_number(substr(g.ryot_code,1,4)) m_vill, g.mob g_phoneno, 
      m.s_purchy indent_number from sisl_purpurchase m,cn_ryot_mst g where 
      m.unitcode=g.unit_code and lpad(v_code,4,0)||lpad(f_code,5,0)=g.ryot_code And
      length(g.mob)=10 And m.tran_date between '", startdate, "' and '", enddate, 
      "' And nvl(m_sms_send, 0)=0 AND m.unitcode = '",unitcode,"' And rownum <= 10"
   });
   return SqlHelper.ExecuteReader(CnSettings.cnString1, CommandType.Text, commandText);
}
2
  • your SqlHelper.ExecuteReader return wrong type, so or use SqlDataReader insted of OracleDataReader or modify your helper Commented Dec 24, 2013 at 18:15
  • 1
    show code of SqlHelper.ExecuteReader - propably return wrong type Commented Dec 24, 2013 at 18:16

3 Answers 3

2

SqlHelper.ExecuteReader is returning the wrong type - a SqlDataReader; when your getWeightmntInfoForCenter method is expecting to return an OracleDataReader.

If you want to return the SqlDataReader, you could change the signature of this method:

public IDataReader getWeightmntInfoForCenter(...)

This specifies that this method will return a class implementing IDataReader, but doesn't specify precisely what concrete type that will be, which means you can return either an SqlDataReader or an OracleDataReader (as both classes implement this interface).

Sign up to request clarification or add additional context in comments.

4 Comments

Your solution is nice but what if he needs a specific type method like: reader.GetOracleString(0);
@YairNevet Then he should go with one of the other answers ;) - though, saying that, based on the name of the method, any calling method should be independent of implementation-specific details like GetOracleString.
Or maybe to up-cast it: getWeightmntInfoForCenter(...) as OracleDataReader
@YairNevet Yes, casting would be an option but would generally suggest something has been designed badly somewhere - you shouldn't call a method that needs an IDataReader if you need something that might not be available on the object that's returned, otherwise it's pointless using the interface in the first place.
0

SqlHelper.ExecuteReader() returns an object of type SqlDataReader but the return type of your method is OracleDataReader which is different.

You could change your method signature to return DbDataReader instead. Both specific data reader classes for Oracle and Sql Server derive from that generic one:

public DbDataReader getWeightmntInfoForCenter(...)
{
    ...
}

On the other hand if you're working with an Oracle database you can't use SqlHelper.ExecuteReader() because it's a specific implementation for SQL Server. Have a look at OracleCommand.ExecuteReader() instead.

1 Comment

Yes I tried it, but it wont work @Davecz ans is correct. I made changes in my sqlhelper file and it is working fine.
0

Your getWeightmntInfoForCenter method signature return type is OracleDataReader, while you are actually returning SqlDataReader, look:

return SqlHelper.ExecuteReader(...

Change your code to call: OracleCommand.ExecuteReader method so that OracleDataReader will be returned properly.

Do it like that:

    public OracleDataReader GetWeightmntInfoForCenter(string startdate, string enddate, string unitcode)
    {
        string commandText = string.Concat(new[]
            {
                "select m.unitcode unit_code, to_char(tran_date, 'dd-MON-yy') m_date, to_char(m_purchy) m_number, net AS n_weight, to_number(substr(g.ryot_code,5)) m_grow, to_number(substr(g.ryot_code,1,4)) m_vill, g.mob g_phoneno,m.s_purchy indent_number from sisl_purpurchase m,cn_ryot_mst g where m.unitcode=g.unit_code and lpad(v_code,4,0)||lpad(f_code,5,0)=g.ryot_code And length(g.mob)=10 And m.tran_date between '"
                , startdate, "' and '", enddate, "' And nvl(m_sms_send, 0)=0 AND m.unitcode = '", unitcode,
                "' And rownum <= 10"
            });

        using (var connection = new OracleConnection(CnSettings.cnString1))
        {
            var command = new OracleCommand(commandText, connection);
            connection.Open();
            return command.ExecuteReader();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.