C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Errors and Solutions » General »

SQL BULK COPY error: invalid attempt to call fieldcount when reader is closed Error


Posted Date: 31 Aug 2009    Resource Type: Errors and Solutions    Category: General
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 5



Problem Description:



The error " invalid attempt to call fieldcount when reader is closed" can occur when bulk copying data to Sql server table using SqlBulkCopy.



Consider the following example:



The following example throws the error "Invalid attempt to call fieldcount when reader is closed"



Example:



' Retrieval of the data from the source ODBC connection table




Dim sODBCCmd As New OdbcCommand(strODBC, sODBCConn)



sODBCConn.Open()



OdbcDr = sODBCCmd.ExecuteReader()



sODBCConn.Close() ' We are Closing the connection



' SQL Bulk Copy



Using bulkCopy As SqlBulkCopy = _



New SqlBulkCopy(targetConnectionString, SqlBulkCopyOptions.TableLock)



bulkCopy.DestinationTableName = "[" & sTableName & "]"



Try



bulkCopy.WriteToServer(OdbcDr)



Catch ex As Exception



Console.WriteLine(ex.Message) ' Error Invalid attempt to call fieldcount when reader is closed



End Try



End Using





Possible Reasons:



The data reader is closed.





sODBCConn.Close() (which closes the connection) after ExecuteReader() is called and thus closing the


datareader (the datareader is tied to the connection).



Remarks: The connection.Close() statement closes the data reader as well.



Solution:



We must either keep the connection open and close it and the reader when you are done with it, or make use of a dataset.




sODBCConn.Open()



OdbcDr = sODBCCmd.ExecuteReader()





Using bulkCopy As SqlBulkCopy = _



New SqlBulkCopy(targetConnectionString, SqlBulkCopyOptions.TableLock)



bulkCopy.DestinationTableName = "[" & sTableName & "]"



Try



' Write from the source to the destination.



bulkCopy.WriteToServer(OdbcDr)



Catch ex As Exception



Console.WriteLine(ex.Message) ' Error Throws Here



Finally



OdbcDr.Close()



sODBCConn.Close() ' Should be closed Here to avoid the error



End Try



End Using





For more details, visit http://vijirajkumar.blogspot.com/2009/08/sql-bulk-copy-error-invalid-attempt-to.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
SQL BULKcopy error  .  SQL BULK COPY error: invalid attempt to call fieldcount when reader is closed Error  .  .net errors and solutions  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Client side validation in javascript
Previous Resource: Cannot access destination table 'table-name' Error – During SqlBulkCopy
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use