Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SqlDataReader Data Class Question
Message
From
28/10/2004 15:47:26
 
General information
Forum:
ASP.NET
Category:
Web forms
Environment versions
Environment:
C# 1.1
OS:
Windows 2000 SP3
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00954923
Message ID:
00955463
Views:
13
Tom,

I think you need to use:

ScrapDB.CloseReader();

~~Bonnie



>Randy;
>
>Thank you for your time and effort to point me in the right direction.
>
>Randy;
>
>I like your idea and am attempting to implement it. Here is the latest:
>
>In the Form Load
>
>DdlUsers.DataSource = ScrapDB.GetUsers();
>DdlUsers.DataTextField = “UsersID”;
>DdlUsers.DataBind();
>
>ScrapDB.CloseReader;
>
>The code fires and populates ddlUsers. An error is thrown in Public static CloseReader() // Under CloseReader(), in my data class and the message is ”“Class, struct, or interface method must have a return type”.
>
>So I tried:
>
>Public void CloseReader()
>{
>	UsersReader.Close();
>}
>
>Again ddlUsers loads and it throws an error on the form at ScrapDB.CloseReader; and the error is:
>“An object reference is required for the nonstatic field, method or property ‘eScrap_C.
>ScrapDB.CloseReader()”
>
>Do you see where I am going wrong? At least I am filling the ddl! :)
>
>Tom
>
>
>
>
>
>>Hey Thomas, the close reader will not run because it is after the return statement. you can do one of two things. store the data from the read into a variable then close the read and return the variable. Or create another method to close the reader and call it after you finish using it in your program.
>>
>>
>>public class ScrapDB
>>	{
>>             public sqldatareader UsersReader
>>
>>		public static SqlConnection GetConnection()
>>		{
>>			string connectionString = ConfigurationSettings.AppSettings["connectionString"];
>>		
>>			return new SqlConnection(connectionString);
>>	
>>		}
>>
>>	public static SqlDataReader GetUsers()
>>				{
>>			// Use a SqlDataReader to fill ddlUsers...
>>			SqlDataReader UsersReader = null ;
>>				
>>					SqlConnection UsersConnection = GetConnection() ;
>>					
>>					string selectStatement = "SELECT UserID "
>>						+ "FROM UserInfo ";
>>			
>>					SqlCommand selectCommand = new SqlCommand(selectStatement, UsersConnection);
>>					//SqlDataReader UsersReader;
>>			
>>					UsersConnection.Open();
>>					UsersReader = selectCommand.ExecuteReader(CommandBehavior.CloseConnection);
>>					UsersReader.Read() ;
>>					return UsersReader ;
>>				
>>				
>>			
>>		
>>
>>			}
>>		public static CloseReader()
>>		{
>>		        UsersReader.Close() ;
>>	
>>		}
>>
>>}
>>
>>
>>
>>
>>
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