Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Method To Fill DropDownList From SQL

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
Cation: I am new to c# and all things .NET so please have mercy.

I am trying to call a method from a non local class (not in the aspx or the code behind). All examples that I find are all putting the code in the the .cs behind the .aspx page. Thats not what I am looking for.

This is what i have while testing in the code behind the .aspx, but I can't seem to figure out how to call and return the data.


Code:
    class UserControlPresenter
    {
        public void GetAuthorizedUsers()
        {
             string selectSQL = "SELECT [user_id], [user_lname] + ', ' + [user_fname] AS userName FROM [user] ORDER BY [user].[user_fname];";
             SqlCommand cmd = new SqlCommand(selectSQL, new SqlConnection(ConfigurationManager.AppSettings["SQLConnectionString"]));
             cmd.Connection.Open();

             SqlDataReader ddlValues;
             ddlValues = cmd.ExecuteReader();

             lst_Users is the DropDownControl from the .aspx page which obviously wont work in this remote method but I thought that I should show you what I had working in the .aspx.

             lst_Users.DataSource = ddlValues;
             lst_Users.DataValueField = "user_id";
             lst_Users.DataTextField = "userName";
             lst_Users.DataBind();

             cmd.Connection.Close();
             cmd.Connection.Dispose();
          }
     }

How do i call this from the pageload for example?


I hope that I have been clear enough in my question.
Thanks in advance for you time.
 
faq855-7190

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Create an instance of the class in your code-behind and then call the method you want.
Something like:
Code:
UserControlPresenter imyClass = new UserControlPresenter(); 
UserControlPresenter.GetAuthorizedUsers();

I don't know C# so the syntax may not be correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top