I keep getting an error: System.Data.SqlClient.SqlDataReader.

Any idea on why this error is arising?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Sql;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string strconn = "SERVER=22.3.1.9;User=CHMSE;password=18465!JFKi8nis;Database=CFOH";
            SqlConnection conn = new SqlConnection(strconn);

            conn.Open();

            SqlCommand cmd = new  SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select address1 from lalocation where servicenumber < 11";

            SqlDataReader reader = cmd.ExecuteReader();

           /* while (reader.Read())
            {
                Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString());
            }
            */
            conn.Close();
            Console.WriteLine(reader);
            Console.ReadLine();
        }
    }
}

The problem is in your Select statement. You only select one value (address1), but in the read you want to retreive two values (reader[0] and reader[1]).
Solution: or you add two values into select statement, or you remove reader[1] from the while loop.
1.:

cmd.CommandText = "select address1, address2 from lalocation where servicenumber < 11";
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())            
                Console.WriteLine(reader[0].ToString() + " " + reader[1].ToString());

2.:

cmd.CommandText = "select address1from lalocation where servicenumber < 11";
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())            
                Console.WriteLine(reader[0].ToString());

You are welcome. You can mark the thread as answered, so we can "close it up", and others can find the solution for the same problem.
bye, bye,

This article has been dead for over six months. Start a new discussion instead.