Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Still trying to resolve errors using C# and a dataclass
Message
 
To
All
General information
Forum:
ASP.NET
Category:
Web forms
Title:
Still trying to resolve errors using C# and a dataclass
Environment versions
OS:
Windows 2000 SP4
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00956670
Message ID:
00956670
Views:
51
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




}
Next
Reply
Map
View

Click here to load this message in the networking platform