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 6174989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:50:50+00:00 2026-05-23T23:50:50+00:00

I have the following scenario which I’ve been using to return a single row

  • 0

I have the following scenario which I’ve been using to return a single row from a SQL database. After I’ve returned the data I’m simply using it to populate fields on my web form.

Example database class:

public class DBAccess
{
    public DataTable ReturnContactInfo(int userID)
    {
        // executes a stored procedure, populates a datatable, then returns it
    }
}

Example using the class to populate a theoretical customer name field:

protected void Page_Load(object sender, EventArgs e)
{
    if(!isPostBack)
    {
        DBAccess db = new DBAccess();
        DataTable dt =  db.ReturnContactInfo(1);
        if (dt.Rows.Count > 0)
        {
           customerName.Text = Convert.ToString(dt.Rows[0]["customerName"]);
        }
     }
}

Is there a better or more efficient way of doing this (“this” being returning a single data row to pull data from)? This approach certainly works, but it’s a bit cumbersome to have to type dt.Rows[0][string] when I’m always and only going to be working with the row at index 0. Please keep in mind that I’d like to be able to keep all the data layer stuff extracted to a single class. Which (from what I understand) means I cannot use a SqlDataReader because it has to be closed when finished so I can’t return it from my data layer class.

  • 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-23T23:50:50+00:00Added an answer on May 23, 2026 at 11:50 pm

    Define a class with the members you want as properties. Then populate and return that object. You can make this simple using tools like “dapper”:

    public Customer GetCustomer(int id)
    {
         return connection.Query<Customer>("getCustomer",
             new {id}, // <=== param
             commandType: CommandType.StoredProcedure).Single();
    }
    

    Then:

    var cust = GetCustomer(12345);
    string name = cust.Name;
    

    Now:

    • it is clear we have a single object
    • the members on the object are obvious and well-types
    • you avoid a lot of overhead from DataTable
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario. I have a search page which is split into
Have the following scenario. I have a few form, which essentially have a few
I have the following scenario: Editor Role should not be allowed to delete nodes.
I just encountered the following situation. I have an Android app with a scenario
Consider the following scenario: You have an account model You have an external service
I am looking for the perfect data structure for the following scenario: I have
We've got the following scenario: Central Database (replicated across multiple servers) Client Database 1
I have the following scenario (note that activity A has launchMode=singleTop ): Activity A
I'd appreciate any advice on which way to go on in the following scenario.
Let's assume we have the following generic scenario: An RDBMS as a data source,

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.