Current location: Hot Scripts Forums » Programming Languages » ASP.NET » invalid operation exception


invalid operation exception

Reply
  #1 (permalink)  
Old 06-08-11, 08:50 AM
altarek's Avatar
altarek altarek is offline
Newbie Coder
 
Join Date: Dec 2010
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
invalid operation exception

i have a question in invalid operation exception. it says that " ExecuteReader Connection is closed and must be open

PHP Code:

SqlConnection conn = new SqlConnection("Server=localhost\\SqlExpress;" +

"Database=db;Integrated Security=True");
            
SqlCommand comm = new SqlCommand("SELECT EmpID, Name FROM Employee"conn);
           
            
SqlDataReader reader comm.ExecuteReader(); 
Reply With Quote
  #2 (permalink)  
Old 06-09-11, 02:27 AM
altarek's Avatar
altarek altarek is offline
Newbie Coder
 
Join Date: Dec 2010
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

ok,the problem solved

add this line before reader

comm.Connection.Open();


PHP Code:

SqlConnection conn = new SqlConnection("Server=localhost\\SqlExpress;" 
"Database=db;Integrated Security=True"); 
            
SqlCommand comm = new SqlCommand("SELECT EmpID, Name FROM Employee"conn); 
            
comm.Connection.Open(); 
            
SqlDataReader reader comm.ExecuteReader(); 
Reply With Quote
  #3 (permalink)  
Old 06-09-11, 09:51 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Yes. You always have to open a database connection before reading from it.

Here is the official Microsoft recommended way of doing it:

Code:
private static void ReadData(string connectionString, string sql)
{
    string queryString = sql;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}", reader[0], reader[1]));
        }

        // Call Close when done reading.
        reader.Close();
    }
}
__________________

Last edited by digioz; 06-09-11 at 09:57 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot find KeycodeV2.dll, or invalid keycode. Please Help Me sankarb Windows .NET Programming 5 11-18-09 05:14 PM
Include problem in C bkbenson C/C++ 4 02-08-05 04:24 AM
calendar working until months changed bitesize JavaScript 1 01-13-04 01:50 PM
changing a specific dateformat in tigra bitesize JavaScript 0 10-30-03 08:58 AM


All times are GMT -5. The time now is 02:47 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.