vikifor
vikifor

Reputation: 465

Invalid operation.The connection is closed error while reading from SQL server

I like to read from database using Linq to SQL. However in this method throw exception

  public string[] readFromAbbrevationsPerson()
{
    string[] resultAbbrevationsPerson = new string[10000];


    DataClassesDataContext db = new DataClassesDataContext("NERMacedonianConnectionString");

    var query = from abb in db.abbrevationsPersons
                select abb.abbrevationsPerson1;
    int i = 0;

    foreach (string noun in query)
        resultAbbrevationsPerson[i++] = noun;

    return resultAbbrevationsPerson;
}

in the foreach statement.The exception is Invalid operation the connection is closed. I made refresh in VS 2010 to the server and I made refresh in SQL Management studio.Ichecked the status of the server is running.

Upvotes: 3

Views: 3778

Answers (1)

kad81
kad81

Reputation: 10950

You need to pass the full connection string to the constructor for DataClassesDataContext(string connection), unless you've created your own constructor which just takes a value like what you've entered. The exception in this case is not very helpful in determining the cause.

Upvotes: 2

Related Questions