locked
how to include the data reader in the function RRS feed

  • Question

  •  Hi,

    I have the code below and it has the error msg 'aReader does not exist' in the private void AddRow() function.  How to pass the parameter 'aReader' into addrow() function?

    Thx



    string sqlQuery = "SELECT * FROM temp" ;
    OleDbCommand aCommand = new OleDbCommand(sqlQuery, Connect);
    OleDbDataReader aReader = aCommand.ExecuteReader();

    AddRow();

      private void AddRow()
      {
           string mname= aReader["name"].ToString();
       }

    Thursday, February 19, 2009 7:17 AM

Answers

  • You have two options.

    1. Declare the aReader as an attribute of your class.
    2. Modify addRow so that it can accept the DataReader as a parameter as shown below

    private void AddRow(OleDbDataReader aReader)
      {
           string mname= aReader["name"].ToString();
       }
    • Marked as answer by Bin-ze Zhao Monday, February 23, 2009 6:51 AM
    Thursday, February 19, 2009 7:31 AM

All replies

  • You have two options.

    1. Declare the aReader as an attribute of your class.
    2. Modify addRow so that it can accept the DataReader as a parameter as shown below

    private void AddRow(OleDbDataReader aReader)
      {
           string mname= aReader["name"].ToString();
       }
    • Marked as answer by Bin-ze Zhao Monday, February 23, 2009 6:51 AM
    Thursday, February 19, 2009 7:31 AM
  • what about the code that i call this function? 
    is it write as below?



    AddRow(aReader);


     private void AddRow(OleDbDataReader aReader)
      {
           string mname= aReader["name"].ToString();
       }

    • Edited by Brent Serbus Tuesday, February 24, 2009 4:40 AM copy past html issue
    Thursday, February 19, 2009 9:31 AM
  • Yes.
    Thanks, A.m.a.L
    • Proposed as answer by valdimerpenov Friday, March 6, 2009 7:33 AM
    Thursday, February 19, 2009 7:23 PM
  • You may want to have the incoming parameter be of the DbDataReader type, which is the base class for OleDbDataReader, as well as several other data reader classes, including SqlDataReader, DataTableReader, OdbcDataReader, and SqlCeDataReader.  Using the more general base class will enable you to easily switch functionality later on, and will prevent you from being tied down to only one database. 


    David Morton - http://blog.davemorton.net/
    Thursday, February 19, 2009 7:34 PM
    Moderator
  • thank you so much for all your response
    Monday, February 23, 2009 3:54 AM