Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code im getting Error in the blocked line. Please advise how to go about it.

C#
mconn.Open();
using (SqlCommand msqlcmd = new SqlCommand("select * from table1", dlsmain.dlsconn))
{
    using (SqlDataReader msqldat = msqlcmd.ExecuteReader())
    {
        msqldat.Read();
		//...  
		//... assignments from msqldat
		//...
        msqldat.Close();
        msqlcmd.CommandText = "select * from table2";
        msqldat = msqlcmd.ExecuteReader();		// Error : Cannot assign to msqldat because it is a 'using variable'.
        while (msqldat.Read())
        {
			//...
			//...  Some process using msqldat
			//...
        }
        msqldat.Close();
        msqlcmd.CommandText = "select * from table3";
        msqldat = msqlcmd.ExecuteReader();
        while (msqldat.Read())
        {
			//...
			//... Some process using msqldat
			//...
        }
    }
}
mconn.Close();


What I have tried:

googled for the error but not got right channel.
Posted
Updated 2-Aug-16 20:58pm

Try following-
C#
using (SqlDataReader msqldat = msqlcmd.ExecuteReader())
{
        msqldat.Read();
	//...  
	//... assignments from msqldat
	//...
        //msqldat.Close();
} 
msqlcmd.CommandText = "select * from table2"; 
using (SqlDataReader msqldat = msqlcmd.ExecuteReader())
{
        msqldat = msqlcmd.ExecuteReader();		// Error : Cannot assign to msqldat because it is a 'using variable'.
        while (msqldat.Read())
        {
			//...
			//...  Some process using msqldat
			//...
        }
        //msqldat.Close();
}
msqlcmd.CommandText = "select * from table3";
using (SqlDataReader msqldat = msqlcmd.ExecuteReader())
{        
        msqldat = msqlcmd.ExecuteReader();
        while (msqldat.Read())
        {
			//...
			//... Some process using msqldat
			//...
        }
}


Hope, it helps :)
 
Share this answer
 
Comments
Priya-Kiko 3-Aug-16 3:04am    
Thank you so much.

msqldat.Close() is needed though :)
Suvendu Shekhar Giri 3-Aug-16 3:10am    
Yes.. That's why I commented them.
Gald that it helped :)
Priya-Kiko 3-Aug-16 3:33am    
Thank you so much. 5 stars to you.
Karthik_Mahalingam 3-Aug-16 9:41am    
5
Suvendu Shekhar Giri 4-Aug-16 3:17am    
Thanks :)
The message is clear enough. You have declared (and assigned a reference to) msqldat in your using statement, which means that it is effectively read-only inside the block. If you allocated some other value to it then it would be compromised and could not properly dispose the original reference.
 
Share this answer
 
Comments
Priya-Kiko 3-Aug-16 2:51am    
Thank you, So it is that I have to close the using block after msqldat.Close and then again open another using block for the next set of statements ?
Richard MacCutchan 3-Aug-16 3:05am    
Possibly, but that would not necessarily make sense. You need to look at exactly what you are trying to do and adjust your code logically.
Priya-Kiko 3-Aug-16 3:33am    
Thank you.
C#
You can't change reference once you used in using clause. I don't thing you require using there, its dis-connected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900