Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:02:15+00:00 2026-05-30T12:02:15+00:00

I have to read an sql table with about 100 columns and then check

  • 0

I have to read an sql table with about 100 columns and then check each cell to verify if the data is not null. Using SQLReader it throws me an IndexOutOfBoundsOfArrway exception at column no 70. Any ideea how shold I reach my goal?
P.S. I am doing this in c#

This is my code:

conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand( "select * from tableName where column1='" + 
                                 textBox1.Text+"'", conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
   for (int i = 3; i < 100; i++)
   {
      if (reader[i].ToString() != "")
      {
          listBox2.Items.Add(reader[i].ToString());
      }

    }
 }

EDIT: I am sure that my table has 100 column, but reader.FieldCount is equal to 70

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:02:16+00:00Added an answer on May 30, 2026 at 12:02 pm

    Try this more dynamic approach, it will work no matter the amount of columns:

    while (reader.Read())
    {
       // not sure  why you started at 3, 
       // this code is generic so it will also work for tables with less that 3 columns
       // but maybe that is something you can omit
       for (int i = 0; i < reader.FieldCount ; i++)
       {
          if (reader[i] == null)
             continue;
    
          listBox2.Items.Add(reader[i].ToString());      
       }
    }
    

    You might also want to consider moving the null check out of the actual reading procedure since looping through the columns takes a considerable amount of time and your blocking the reader while you do so.

    However depending on the amount of rows your reading in this might introduce new challenges in regards to memory consumption.

    UPDATE
    In order to avoid retrieving each column separately by index, use the GetValues() batch operation of the DataReader instead that returns an object array of all values, also if you expect just one line, use if rather than while to avoid issues:

    if (reader.Read())
    {
      var values = new object[reader.FieldCount];
      reader.GetValues(values);
    
      for (int i = 0; i < values.Count ; i++)
      {
        if (values[i] == null)
           continue;  
        listBox2.Items.Add(values[i].ToString());      
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read that while plug-ins are not supported for SQL Server Management Studio,
I have read these very good questions on SO about SQL stored procedures: When
I have a table where new data gets filled each time a user clicks
I have read about partial methods in the latest C# language specification , so
I have read this post about how to test private methods. I usually do
Here is my error information : CREATE TABLE `mb_topics` ( `id` mediumint(9) NOT NULL
I have a question about why some SQL (running on SQL Server 2005) is
I have a database table (SQL Server 2008 R2 Express edition) that has a
In .NET 3.5sp1, I have a table in Sql Server that is defined somehow
I read this already: The Best Way to shred XML data into SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.