Populate 2 or more datagrids from one OleDBCommand

Discussion in 'ASP .Net' started by Simon Harris, Jan 30, 2005.

  1. Simon Harris

    Simon Harris Guest

    Hi All,

    I wish to populate more than one datagrid from the same OleDBCommand. The
    code I have is:

    Dim objCmd As New OleDbCommand(strSql, objConn)

    Then...

    Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
    Me.dgTariffHolidayHomesBand1.Visible = True
    Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
    Me.dgTariffHolidayHomesBand2.Visible = True

    I get this error when the second call to objCmd.ExecuteReader() is called:
    System.InvalidOperationException: ExecuteReader requires an open and
    available Connection.

    The reason for populating multiple grids from the same data, is that I am
    calling ALL columns in the SQL, and then using (for example) columns A and
    B
    in DG1, C and D in DG2...and so on.

    Surely I don't need a new recordset for each datagrid???

    Simon.
     
    Simon Harris, Jan 30, 2005
    #1
    1. Advertising

  2. Simon Harris

    DalePres Guest

    There are a couple problems with your approach.

    First, the ExecuteReader method requires that you close and re-open the
    connection, just as the code implies but you can't use a DataReader as the
    DataSource for a DataGrid. You need something that implements IList or
    IListSource. You can research both of those interfaces in the MSDN Library
    to see what classes implement them.

    Another problem is that you're making two queries to the database and
    populating the DataGrids sequentially. What happens if the data changes
    between calls? You can't be sure the data in the DataGrids will match.
    What you need to do is get the data once, using a DataSet or even a
    DataTable and set both DataGrids to use the same DataSource.

    Hope this helps,

    DalePres
    MCAD, MCSE, MCDBA


    "Simon Harris" <> wrote in message
    news:...
    > Hi All,
    >
    > I wish to populate more than one datagrid from the same OleDBCommand. The
    > code I have is:
    >
    > Dim objCmd As New OleDbCommand(strSql, objConn)
    >
    > Then...
    >
    > Me.dgTariffHolidayHomesBand1.DataSource = objCmd.ExecuteReader()
    > Me.dgTariffHolidayHomesBand1.Visible = True
    > Me.dgTariffHolidayHomesBand2.DataSource = objCmd.ExecuteReader()
    > Me.dgTariffHolidayHomesBand2.Visible = True
    >
    > I get this error when the second call to objCmd.ExecuteReader() is called:
    > System.InvalidOperationException: ExecuteReader requires an open and
    > available Connection.
    >
    > The reason for populating multiple grids from the same data, is that I am
    > calling ALL columns in the SQL, and then using (for example) columns A and
    > B
    > in DG1, C and D in DG2...and so on.
    >
    > Surely I don't need a new recordset for each datagrid???
    >
    > Simon.
    >
     
    DalePres, Jan 31, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel

    OleDbDataAdapter vs OleDbCommand

    darrel, Nov 23, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,014
    Scott M.
    Nov 25, 2005
  2. Tom Wild
    Replies:
    0
    Views:
    401
    Tom Wild
    Aug 21, 2003
  3. =?Utf-8?B?SmFzb24gU3RlZXZlcw==?=

    OleDbCommand Selecting Variable Data

    =?Utf-8?B?SmFzb24gU3RlZXZlcw==?=, Jun 27, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    509
    =?Utf-8?B?SmFzb24gU3RlZXZlcw==?=
    Jun 27, 2005
  4. Mark Sandfox

    Problem with OleDBCommand Update

    Mark Sandfox, Jul 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,043
    Balder78
    Aug 13, 2008
  5. =?Utf-8?B?Q2hyaXMgRmluaw==?=

    OleDbCommand TimeOut Not Working

    =?Utf-8?B?Q2hyaXMgRmluaw==?=, Aug 31, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,304
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Aug 31, 2005
Loading...

Share This Page