Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Still trying to resolve errors using C# and a dataclass
Message
From
01/11/2004 16:38:59
 
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:
00956726
Views:
7
Well, Tom, I'm a bit confused, because I know we went over this before already and with the answers that Randy gave you and that I gave you, I thought that you already had all this solved. I'm confused as to why you posted all this again?!?!

~~Bonnie




>Bonnie;
>
>I will post what finally worked. Randy had the idea - it was a matter of syntex.
>
>Tom
>
>>Tom,
>>
>>Since when has it ever made any sense to have code execute *after* a return? I'm surprised that VB does not throw a compiler error! However, I bet that the code in VB doesn't ever actually close the DataReader either.
>>
>>~~Bonnie
>>
>>
>>
>>>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
>>>
>>>
>>>
>>>
>>> }
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform