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 19:25:09
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
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:
00956793
Views:
7
Thomas,
Return the reader object.
Unlike dataadapter Fill (analogy select-SQL in VFP) it reads records one by one (analogy scan..endscan in VFP).

while (reader.Read())
// Do something with DataRow

It also has a close method which you should call to close the reader when you're done.

return UsersReader;
UsersReader.Close(); // Error – Unreachable code detected

After return UsersReader.Close(); line is unreachable. Simply remove it. In code where you get UsersReader object have that line to close the connection.

I don't have much idea about VB.Net but probably it's not erroring since you've CommandBehavior.CloseConnection and that imlplicitly closes the connection when done.


drUsers = cmdUserID.ExecuteReader(CommandBehavior.CloseConnection)

drUsers.Read()
Return drUsers

Here you get the object, only read first DataRow (and do nothing with it) then return the reader object.


SqlCommand selectCommand = new SqlCommand(selectStatement, GetConnection());

Here from this code GetConnection() stands as a method returning a connection object. Then:

GetConnection.Close()

should be:

GetConnection().Close()

or it's like this:

SqlConnection cn = GetConnection();
//...
cn.Close();
public void ddlFiller()
{
   // Use a SqlDataReader to fill ddlUsers...

   SqlDataReader UsersReader = 
       GetUsers("SELECT UserID FROM UserInfo ORDER BY UserID"); // Get reader object
   
   while (UsersReader.Read()) // Loop through all rows
        { // Fill the ddl via UsersReader["UserID"] }
   UsersReader.Close(); // Closes the connection implicitly - CommandBehavior.CloseConnection 
}

public static SqlDataReader GetUsers(string strSQL)
{
	SqlConnection UsersConnection = GetConnection() ; 			
	UsersConnection.Open();

	SqlCommand selectCommand = new SqlCommand(strSQL, UsersConnection);
	SqlDataReader UsersReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);

	return UsersReader;
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform