Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Still trying to resolve errors using C# and a dataclass
Message
General information
Forum:
ASP.NET
Category:
Web forms
Environment versions
OS:
Windows 2000 SP4
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00956670
Message ID:
00956723
Views:
7
If you attempt to close the reader before the RETURN you get the error:

Invalid attempt to Field Count when reader is closed. I think I have the solution thanks to several people in my previous post about this topic.

>In your code:
>
>return UsersReader;
>UsersReader.Close(); // Error – Unreachable code detected
>
>
>Isn't the code after a return never executes? You tell to return. No lines after the return can be executed!
>
>
>>ASP.NET VB.NET and C# Windows 2000 SP4 SQL Server 2000
>>
>>I thought I had better start over and give the latest results. I tried VB.NET and C#. My hope is to convert my VB.NET application to C#.
>>
>>First in VB.NET:
>>VB.NET – Works as expected – throws no errors.
>>
>>
>>Public Shared Function FillUsers() As SqlDataReader
>>        ' Fill User ddl using a SqlDataReader...
>>
>>        Dim sSelect As String = "SELECT * FROM UserInfo ORDER BY UserID"
>>        Dim dbConnection As SqlConnection = GetConnection()
>>        dbConnection.Open()
>>
>>        Dim cmdUserID As New SqlCommand(sSelect, dbConnection)
>>
>>        Dim drUsers As SqlDataReader
>>        drUsers = cmdUserID.ExecuteReader(CommandBehavior.CloseConnection)
>>
>>        drUsers.Read()
>>        Return drUsers
>>
>>        drUsers.Close() ‘ No error at this point…
>>
>>    End Function
>>
>>
>>C# and a SqlDataReader
>>
>>public static SqlDataReader GetUsers()
>>		{
>>			// Use a SqlDataReader to fill ddlUsers...
>>			string selectStatement = "SELECT UserID "
>>				+ "FROM UserInfo ORDER BY UserID";
>>				
>>			SqlConnection UsersConnection = GetConnection() ; 			
>>			UsersConnection.Open();
>>
>>			SqlCommand selectCommand = new SqlCommand(selectStatement, UsersConnection);
>>			SqlDataReader UsersReader;
>>
>>			UsersReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
>>
>>			UsersReader.Read();
>>
>>			return UsersReader;
>>
>>			UsersReader.Close(); // Error – Unreachable code detected
>>
>>		}
>>
>>Ok – lets try a DataTable in C#
>>
>>
>>public static DataTable GetUsers()
>>		{
>>			// Use a DataTable to fill ddlUsers...
>>			
>>			string selectStatement = "SELECT UserID "
>>				+ "FROM UserInfo ORDER BY UserID";
>>
>>			SqlCommand selectCommand = new SqlCommand(selectStatement, GetConnection());
>>
>>			SqlDataAdapter daUsers = new SqlDataAdapter();
>>			daUsers.SelectCommand = selectCommand;
>>			DataSet dsUsers = new DataSet();
>>			daUsers.Fill(dsUsers, "UserInfo");
>>		
>>			return dsUsers.Tables["UserInfo"] ;
>>
>>			GetConnection.Close();  // Two Errors – 1.  eScrap_C.ScrapDB.GetConnection()’ denotes a ‘method’ which is not valid in the given context
>>
>>2.	Unreachable code detected
>>
>>
>>Something basic must be incorrect. If you see the problem please tell me. In VB.NET this is straight forward and works – if I want to close a connection or DataReader I can without throwing errors.
>>
>>Thank God it is Monday! That gives me four more days to pull out my hair before the weekend! :)
>>
>>Tom
>>
>>
>>
>>
>> }
Previous
Reply
Map
View

Click here to load this message in the networking platform