Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Listing members from database

    Hi,

    I am currently building a website and I plan to show list of members that are registered on the website, I need some insight on how I would that with snippets of sample code if possible.

    My idea is to have a loop that goes through the data base, in the loop the Labels would be created with the member names. Would this be a good method?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    Hey jt9000,

    There are so many ways of doing this that I don't even know where to begin, and of course it all depends on the situation. Here's one I like which will probably suit your situation:

    Let's say that on the .aspx page, you have a data control, such as a Repeater, named "Repeater_Results". Well in your code-behind, you would
    have something like this, which would bind your data to the Repeater.

    By the way, here is a link about data controls, which I'm guessing you're not familiar with either: http://quickstarts.asp.net/QuickStar...a/default.aspx

    And here's an even better link, whose example binds data to a Repeater in a very similar fashion as my snippet below, except it takes data from an XML file, and first saves it in a DataSet : http://www.w3schools.com/aspnet/aspnet_repeater.asp

    Code:
    using (SqlConnection mySqlConnection = new SqlConnection("[my connection string]"))
    {
    	mySqlConnection.Open();
            using (SqlCommand mySqlCommand = new SqlCommand("[my stored procedure call]", mySqlConnection))
            {
            	using (SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
                    {
    			mySqlCommand.CommandType = CommandType.StoredProcedure;
                            Repeater_Results.DataSource = mySqlDataReader;
                            Repeater_Results.DataBind();
    		}
            }
            mySqlConnection.Close();
    }
    Regards,
    Mike

  • #3
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts
    ...forgot to mention, the SQLCommand, SQlConnection, etc are all part of namespace System.Data.SqlClient


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •