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

  • SEARCH
  • Home
  • 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 6852949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:27:10+00:00 2026-05-27T01:27:10+00:00

I know there’s a cleaner way to do this than the way I am.

  • 0

I know there’s a cleaner way to do this than the way I am. Basically, I retrieve a datatable from my sql database. I now want to have that information in a class that is globally accessible. I don’t want to have to go through each column like so: txtFirstName.Text = dt[0].ToString(). I want to create a “User” class, and assign txtFirstName.Text = User.FirstName. BUT, I don’t want to manually map dt[0] to FirstName, and dt[1] to LastName…I want the object to be created automagically! Or, at least once the class is created, and the elements match the names of the dt columns, the mapping should happen automatically.

Thanks!

  • 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-27T01:27:11+00:00Added an answer on May 27, 2026 at 1:27 am

    Why not use Linq to SQL? I like it alot.

    Once you draw out your tables you can then create a separate partial .cs class file for individual tables in this case. DO NOT EDIT THE GENERATED CODE 🙂 If you saved your dbml file in lets say “App_Code/Linq_customer/” Folder and mapped a table you named “Customer”, your class may look like this (untested code):

    using System.Linq;
    
    namespace Linq_customer
    {
        public partial class Customer
        {
            public static Customer GetCustomerByID(int customerID, CustomerDataContext dc)
            {
    
                return (from s0 in dc.Customers
                       where s0.customerID== customerID
                       select s0).firstOrDefault();
            }
        }
    }
    

    Then from your page or other class do something like:

    using Linq_customer;
    ...
    CustomerDataContext dc = new CustomerDataContext() //(Generated data context)
    Customer myCustomerDude = Customer.GetCustomerByID(5, dc);
    if(myCustomerDude == null) return error..
    txtFirstName.Text = myCustomerDude.firstName;
    

    Optionally if you wanted to create a new user:

    myCustomerDude = new Customer();
    

    And saving any new info is fairly easy too:

    …

    myCustomerDude.City = txtCity.Text;
    myCustomerDude.favoriteSlogan = "I believe in Science";
    dc.Customers.insertOnSubmit(myCustomerDude); //If not already existing, if is already in table then omit this line.
    dc.submitChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are many threads about calculating from lat/long to metric systems. This
I know there should be an easy way to do this but I guess
I know there must be a way to do this, and perhaps someone has
I know there is a lot on this topic but I can't get any
I know there are a lot of questions around on this subject, but I've
i know there is many types of encode and decode and from what i
I know there are lots of posts similar to this, but after crawling SO,
I know there is another question about this here but this just isn't working
I know there are similar questions about this issue (for example, here and here
I know there is a way of finding the sum of digits of 100!(or

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.