CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2009
    Posts
    18

    [RESOLVED] OleDbDataReader not pulling all results

    Can't seem to get all the results from my reader to add into my ArrayList

    Here is the database info
    Code:
    Table: Servers
    Field1: ID *Auto Gen ID field*
    Field2: CleintID
    Field3: SName
    Field4: SIP
    
    Data Stored within table
    1 | 1234 | TestServer1 | 172.17.0.10
    2 | 1234 | TestServer2 | 172.17.0.20
    With the following code my ArrayList count "should" equal 4 but I get 2 and the results stored are as followed
    DeHolder[0] contains TestServer1
    DeHolder[1] contains 172.17.0.20
    Why am I not getting entry 1's SIP and entry 2's SName ?
    The contents of EncHolder matches so its nothing to do with my Decrypt method.

    Here is the code Field1 = SName | Field2 = SIP | Table = Servers | AccountID = 1234
    Code:
    int x = 0;
    ArrayList EncHolder = new ArrayList();
    ArrayList DeHolder = new ArrayList();
    Global.cmd.CommandText = "SELECT " + Field1 + ", " + Field2 + " FROM " + Table + " WHERE ClientID ='" + AccountID + "'";
    OleDbDataReader reader = Global.cmd.ExecuteReader();
    while (reader.Read())
         {
             EncHolder.Add(reader.GetValue(x).ToString());
             DeHolder.Add(Crypto.Decrypt(reader.GetValue(x).ToString(), true));
             x = x + 1;
         }            
    reader.Close();
    What am I missing?
    Thanks in advance
    Last edited by Omegadarkest; May 5th, 2011 at 11:05 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: OleDbDataReader not pulling all results

    Not sure why you think you would get the entire row back. Have you looked at your SQL?

    Code:
    "SELECT " + Field1 + ", " + Field2 + " FROM " + Table + " WHERE ClientID ='" + AccountID + "'";
    Obviously that will only return results for the Field1 and Field2 columns... that's how a SELECT statement works. It says "select these columns from this table with an optional condition". If you want the entire row use SELECT * FROM some_table WHERE <...>.

    Also, your where clause is setup so that you will only get back a single result, i.e., WHERE clientId == someClientId. Obviously that will not return 2 rows given your schema.
    Last edited by BigEd781; May 6th, 2011 at 03:04 PM.

  3. #3
    Join Date
    Dec 2009
    Posts
    18

    Re: OleDbDataReader not pulling all results

    When I run a SQL query in the database I get my desired results (Both rows containing SName and SIP when my accountid = 1234, Why would that change issuing the same commands in C#?

    In the server table I have multiple entries containing the same AccountID so I want to read only the column SName and SIP from all rows

    So I select SName and SIP from Servers and want all results where all rows match accountid, how is my statement wrong? And I am getting 2 rows but I am missing Column2 (SIP) from row 1 and Column1 (SName) from row 2.

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: OleDbDataReader not pulling all results

    Unless the database is broken, you run different SQL in it. As BigEd781 has said, you ask the DB for just two columns, so it returns just two columns and these are the two you see. If you want the other two, you have to ask fot them too.

    And also, if I look well, you have issue in the read loop, because after reading the first row, you will ask for non existent columns at position greater then 2.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured