Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 196379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:45:44+00:00 2026-05-11T16:45:44+00:00

This is going to be very simple I know. I have seen so many

  • 0

This is going to be very simple I know. I have seen so many different ways of using sql in asp.net with no real standard. What I want to know is how to cleanly select from an sql database in asp.net and retrieve multiple records. For example: select all userids.

String sql = 
  "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";

string strCon = System.Web
                      .Configuration
                      .WebConfigurationManager
                      .ConnectionStrings["SocialSiteConnectionString"]
                      .ConnectionString;

SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();

/*
 This is where I need to know how to retrieve the information from the
 above command(comm). I am looking for something similiar to php's
 mysql_result. I want to access the records kind of like an array or some
 other form of retrieving all the data.
 Also when the new SqlCommand is called...does that actual run the
 SELECT STATEMENT or is there another step. 
*/

conn.Close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:45:44+00:00Added an answer on May 11, 2026 at 4:45 pm

    I think that this is what you are looking for.

    String sql = "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";
    
    string strCon = System.Web
                          .Configuration
                          .WebConfigurationManager
                          .ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
    
    SqlConnection conn = new SqlConnection(strCon);
    SqlCommand comm = new SqlCommand(sql, conn);
    conn.Open();
    SqlDataReader nwReader = comm.ExecuteReader();
    while (nwReader.Read())
    {
        int UserID = (int)nwReader["UserID"];
        // Do something with UserID here...
    }
    nwReader.Close();
    conn.Close();
    

    I do have to say, though, that the overall approach can use a lot of tuning. First, you could at least start by simplifying access to your ConnectionString. For example, you could add the following to your Global.asax.cs file:

       using System;
       using System.Configuration;
    
       public partial class Global : HttpApplication
       {
          public static string ConnectionString; 
    
          void Application_Start(object sender, EventArgs e)
          { 
              ConnectionString = ConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
          }
          ...
        }
    

    Now, throughout your code, just access it using:

    SqlConnection conn = new SqlConnection(Global.ConnectionString);
    

    Better yet, create a class in which the “plumbing” is hidden. To run the same query in my code, I’d just enter:

            using (BSDIQuery qry = new BSDIQuery())
            {
                SqlDataReader nwReader = qry.Command("SELECT...").ReturnReader();
                // If I needed to add a parameter I'd add it above as well: .ParamVal("CurrentUser")
                while (nwReader.Read())
                {
                    int UserID = (int)nwReader["UserID"];
                    // Do something with UserID here...
                }
                nwReader.Close();
            }
    

    This is just an example using my DAL. However, notice that there is no connection string, no command or connection objects being created or managed, just a “BSDIQuery” (which does lots of different things in addition to that shown). Your approach would differ depending on the tasks that you do most often.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this one is going to be a long-shot, but I thought I'd
I may be going about this backwards... I have a class which is like
This is going to sound like a silly question, but I'm still learning C,
This is going to be the noobist of all noob questions, but what exactly
This is going to be hard to explain but I'll try my best. I
Is this functionality going to be put into a later Java version? Can someone
This is likely going to be an easy answer and I'm just missing something,
We currently use VSS 6, this is not going to change I am afraid.
i'm trying to make the following routes .. and currently i'm going about this
This is driving me crazy. I'm not going to take it anymore. I'm going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.