data reader question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    data reader question

    I'm using 1 connection to open 3 separate data readers to read data into
    3 data grids. However, when I try to use the 2nd reader it says that
    the first reader must be closed. So I use Close() to close it and now
    it says that 'Object reference not set to an instance of an object' on
    that line of code. Can anybody help me out with this?

    SqlConnection objConnection = new
    SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
    );

    //Associated Call Types
    string strAssocCallTyp es;

    strAssocCallTyp es = "SELECT CallType AS 'Call Type', Remark AS
    'Description' FROM CallTypeSwitch ";
    strAssocCallTyp es += "WHERE OSValue = " + intIncomingRout e;

    //You must open the connection before populating the DataReader
    objConnection.O pen();

    SqlCommand objCommand = new SqlCommand(strA ssocCallTypes,
    objConnection);

    //Create/Populate the DataReader
    SqlDataReader objDataReader = null;

    dgAssocCallType s.DataSource = objCommand.Exec uteReader();
    dgAssocCallType s.DataBind();

    //can only have 1 data reader open at any one time in 1 connection
    objDataReader.C lose();


    Cheers,

    Mike


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • niceguy

    #2
    Re: data reader question

    It seems to me that you never create a SqlDataReader instance in the
    objDataReader variable. You declare it, but in the next line, you pass the
    result of ExecuteReader (which gives you back an instance of SqlDataReader)
    to dgAssocCallType s.DataSource. So objDataReader will still be null. To set
    the datareader variable, you can do this:

    objDataReader = objCommand.Exec uteReader();

    Hope it helps...


    "Mike P" <mrp@telcoelect ronics.co.uk> wrote in message
    news:OIRBASMXDH A.536@TK2MSFTNG P10.phx.gbl...[color=blue]
    > I'm using 1 connection to open 3 separate data readers to read data into
    > 3 data grids. However, when I try to use the 2nd reader it says that
    > the first reader must be closed. So I use Close() to close it and now
    > it says that 'Object reference not set to an instance of an object' on
    > that line of code. Can anybody help me out with this?
    >
    > SqlConnection objConnection = new
    > SqlConnection(C onfigurationSet tings.AppSettin gs["strConnectTran sitTest"]
    > );
    >
    > //Associated Call Types
    > string strAssocCallTyp es;
    >
    > strAssocCallTyp es = "SELECT CallType AS 'Call Type', Remark AS
    > 'Description' FROM CallTypeSwitch ";
    > strAssocCallTyp es += "WHERE OSValue = " + intIncomingRout e;
    >
    > //You must open the connection before populating the DataReader
    > objConnection.O pen();
    >
    > SqlCommand objCommand = new SqlCommand(strA ssocCallTypes,
    > objConnection);
    >
    > //Create/Populate the DataReader
    > SqlDataReader objDataReader = null;
    >
    > dgAssocCallType s.DataSource = objCommand.Exec uteReader();
    > dgAssocCallType s.DataBind();
    >
    > //can only have 1 data reader open at any one time in 1 connection
    > objDataReader.C lose();
    >
    >
    > Cheers,
    >
    > Mike
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]


    Comment

    Working...