SqlDataReader question

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

    #1

    SqlDataReader question

    I'm having a problem where I exec a stored procedure (which SELECTs all rows
    from a table, which has 100 rows) - each row has 8 columns.

    When I exec the code (see below) I get 8 in oSQLDR.FieldCou nt (which I
    expect but whenever I try and reference the data in the row
    (oSQLDR.GetInt3 2(0);) I get an exception "invalid attempt to read when no
    data is present". If I exec the SP in an ISQL window using: EXEC getEvents,
    I get 100 rows returned.

    What am I missing here ?


    <snip>

    oSqlCmd = new SqlCommand();
    oSqlCmd.Connect ion = m_oSqlConnectio n; // A valid connection Object
    is here
    oSqlCmd.Command Text = "getEvents" ;
    oSqlCmd.Command Type = CommandType.Sto redProcedure;

    SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
    int iRows = oSQLDR.FieldCou nt;
    iRowsAffected = oSQLDR.RecordsA ffected;

    int iID = oSQLDR.GetInt32 (0);

    <snip>

    - peteZ


  • Wim Hollebrandse

    #2
    Re: SqlDataReader question

    You need to call the Read() method first on your SqlDataReader object, to
    advance to the first row in your result set. Likewise for subsequent
    records.

    Use a loop to read through all the rows returned, like so:

    while (SQLDR.Read())
    {
    // do something and access SQLDR.GetInt32( 0)...
    }

    Hope that helps.

    Cheers,
    Wim Hollebrandse
    Zealand IT Ltd is a UK based IT consultancy and software development company that specialises in bespoke Microsoft .NET solutions including ASP.NET web applications and advanced SOA architectures for public and private sector clients. Please contact us for a free quote.



    "PeteZ" <peteZ@aol.co m> wrote in message
    news:eLmEGz0bDH A.2672@tk2msftn gp13.phx.gbl...[color=blue]
    > I'm having a problem where I exec a stored procedure (which SELECTs all[/color]
    rows[color=blue]
    > from a table, which has 100 rows) - each row has 8 columns.
    >
    > When I exec the code (see below) I get 8 in oSQLDR.FieldCou nt (which I
    > expect but whenever I try and reference the data in the row
    > (oSQLDR.GetInt3 2(0);) I get an exception "invalid attempt to read when no
    > data is present". If I exec the SP in an ISQL window using: EXEC[/color]
    getEvents,[color=blue]
    > I get 100 rows returned.
    >
    > What am I missing here ?
    >
    >
    > <snip>
    >
    > oSqlCmd = new SqlCommand();
    > oSqlCmd.Connect ion = m_oSqlConnectio n; // A valid connection Object
    > is here
    > oSqlCmd.Command Text = "getEvents" ;
    > oSqlCmd.Command Type = CommandType.Sto redProcedure;
    >
    > SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
    > int iRows = oSQLDR.FieldCou nt;
    > iRowsAffected = oSQLDR.RecordsA ffected;
    >
    > int iID = oSQLDR.GetInt32 (0);
    >
    > <snip>
    >
    > - peteZ
    >
    >[/color]


    Comment

    • Wim Hollebrandse

      #3
      Re: SqlDataReader question

      In addition to my last post. If you only want the first record:

      if (SQLDR.Read())
      {
      Console.WriteLi ne("My value: {0}",SQLDR.GetI nt32(0));
      }
      else
      {
      Console.WriteLi ne("No records found.");
      }

      Cheers,
      Wim Hollebrandse
      Zealand IT Ltd is a UK based IT consultancy and software development company that specialises in bespoke Microsoft .NET solutions including ASP.NET web applications and advanced SOA architectures for public and private sector clients. Please contact us for a free quote.



      "PeteZ" <peteZ@aol.co m> wrote in message
      news:eLmEGz0bDH A.2672@tk2msftn gp13.phx.gbl...[color=blue]
      > I'm having a problem where I exec a stored procedure (which SELECTs all[/color]
      rows[color=blue]
      > from a table, which has 100 rows) - each row has 8 columns.
      >
      > When I exec the code (see below) I get 8 in oSQLDR.FieldCou nt (which I
      > expect but whenever I try and reference the data in the row
      > (oSQLDR.GetInt3 2(0);) I get an exception "invalid attempt to read when no
      > data is present". If I exec the SP in an ISQL window using: EXEC[/color]
      getEvents,[color=blue]
      > I get 100 rows returned.
      >
      > What am I missing here ?
      >
      >
      > <snip>
      >
      > oSqlCmd = new SqlCommand();
      > oSqlCmd.Connect ion = m_oSqlConnectio n; // A valid connection Object
      > is here
      > oSqlCmd.Command Text = "getEvents" ;
      > oSqlCmd.Command Type = CommandType.Sto redProcedure;
      >
      > SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
      > int iRows = oSQLDR.FieldCou nt;
      > iRowsAffected = oSQLDR.RecordsA ffected;
      >
      > int iID = oSQLDR.GetInt32 (0);
      >
      > <snip>
      >
      > - peteZ
      >
      >[/color]


      Comment

      • PeteZ

        #4
        Re: SqlDataReader question

        wim

        thanks mate

        peteZ

        "Wim Hollebrandse" <wimATwimdows.c om> wrote in message
        news:%23Rv72e1b DHA.1492@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > In addition to my last post. If you only want the first record:
        >
        > if (SQLDR.Read())
        > {
        > Console.WriteLi ne("My value: {0}",SQLDR.GetI nt32(0));
        > }
        > else
        > {
        > Console.WriteLi ne("No records found.");
        > }
        >
        > Cheers,
        > Wim Hollebrandse
        > http://www.wimdows.com
        > http://www.wimdows.net
        >
        > "PeteZ" <peteZ@aol.co m> wrote in message
        > news:eLmEGz0bDH A.2672@tk2msftn gp13.phx.gbl...[color=green]
        > > I'm having a problem where I exec a stored procedure (which SELECTs all[/color]
        > rows[color=green]
        > > from a table, which has 100 rows) - each row has 8 columns.
        > >
        > > When I exec the code (see below) I get 8 in oSQLDR.FieldCou nt (which I
        > > expect but whenever I try and reference the data in the row
        > > (oSQLDR.GetInt3 2(0);) I get an exception "invalid attempt to read when[/color][/color]
        no[color=blue][color=green]
        > > data is present". If I exec the SP in an ISQL window using: EXEC[/color]
        > getEvents,[color=green]
        > > I get 100 rows returned.
        > >
        > > What am I missing here ?
        > >
        > >
        > > <snip>
        > >
        > > oSqlCmd = new SqlCommand();
        > > oSqlCmd.Connect ion = m_oSqlConnectio n; // A valid connection[/color][/color]
        Object[color=blue][color=green]
        > > is here
        > > oSqlCmd.Command Text = "getEvents" ;
        > > oSqlCmd.Command Type = CommandType.Sto redProcedure;
        > >
        > > SqlDataReader oSQLDR = oSqlCmd.Execute Reader();
        > > int iRows = oSQLDR.FieldCou nt;
        > > iRowsAffected = oSQLDR.RecordsA ffected;
        > >
        > > int iID = oSQLDR.GetInt32 (0);
        > >
        > > <snip>
        > >
        > > - peteZ
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...