Saturday, 31 August 2013

Close The Reader First Problem in Asp.net When Read Data From SQL Server

Yesterday I was trying to fetch data from SQL server with the help of SqlDataReader but I got an error message "open DataReader associated with this Command which must be closed first " I search a lot on internet and got a solution regarding this problem.

It was advised by someone that when you are reading data from server and trying to insert, delete or update action with the same connection then you will face such error which will told you to close the reader first.

To get the solution for this problem you should do two things.
1. Use SqlDataAdapter and Fill command when you do not want to make an other connection for insertion and other things other than read.

2. use separate sql connection for insert, delete or update action. i.e when you have a SQL Connection 'Con' for reader. Then you should use other SQL Connection for other action.

Someone also advised to add MultipleActiveResultSets=Truein connection string.
Hope this will help you a lot...