LINQ to SQL-pubs ConsoleApplication in Visual C# 2008 Express: FormatException was unhandled-Index (zero based) must be greater than or equal to zero and less than the size of the argument list RRS feed

  • Question

  • Hi all,

    In my Visual C# 2008 Express, I created a Console Application "LINQ-ConsoleLee439" with the following code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.ComponentModel;
    using System.Data;
    namespace LINQ_ConsoleLee439
        class Program
            static void Main(string[] args)
                SqlConnection conn;
                SqlCommand comm;
                SqlDataAdapter adapter;
                DataSet ds = new DataSet();
                //---loads the Authors table into dataset---
                conn = new SqlConnection(@"Data Source=NAB-WK-02554356\SQLSCOTT;" +
                      "Initial Catalog=pubs;Integrated Security=True");
                comm = new SqlCommand("SELECT * FROM Authors", conn);
                adapter = new SqlDataAdapter(comm);
                foreach (DataRow row in ds.Tables[0].Rows)
                    Console.WriteLine("{0} - {1} {2}",
                       row["au_id"], row["au_lname"]);
                //---Using LINQ to DataSet---
                //---query for authors living in CA---
                var authors =
                    from author in ds.Tables[0].AsEnumerable()
                    where author.Field<string>("State") == "CA"
                    select author;

    It complied OK. I executed the project and I got the following error:

                      FormatException was unhandled

                        Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

    I have no ideas how to correct this problem at all.  Please help and advise.


    Scott Chang

    Tuesday, October 20, 2009 1:29 PM


  • I found the mistake I had- I should have the followin 2 code statements: 



    .WriteLine("{0} - {1} {2}",


    "au_id"], row["au_fname"], row["au_lname"]);

    It works OK now.

    Scott Chang 

    • Marked as answer by Yichun_Feng Thursday, October 22, 2009 3:19 AM
    Tuesday, October 20, 2009 2:33 PM