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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:34:31+00:00 2026-05-12T06:34:31+00:00

I have recently come across a very simple Typed DataTable (without using a .XSD)(I’ve

  • 0

I have recently come across a very simple Typed DataTable (without using a .XSD)(I’ve lost author’s URL so I can’t credit him) but it looks like there’s a lot of duplicated code (Such as the Add/Remove/GetNewRow methods).

I’ve tried to push the repetitive methods into a super class, but I have issues due to the Employee needing to be generic. I was hoping to get StackOverflow’s collective hive mind to suggest some ideas to clean this up? (If it’s even possible at all?)

using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace TypedDataSet {

  public class Employees : DataTable {
    protected SqlDataAdapter _adapter;

    public Employees() {
      string connectionString = TypedDataSet.Properties.Settings.Default.ConnectionString;
      _adapter = new System.Data.SqlClient.SqlDataAdapter("SELECT Id, Firstname, Surname FROM Employee", connectionString);
      _adapter.Fill(this);
    }

    public Employee this[int index] {
      get { return (Employee)Rows[index]; }
    }

    public void Add(Employee row) {
      Rows.Add(row);
    }

    public void Remove(Employee row) {
      Rows.Remove(row);
    }

    public Employee GetNewRow() {
      Employee row = (Employee)NewRow();
      return row;
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
      return new Employee(builder);
    }

    public IEnumerator GetEnumerator() {
        return Rows.GetEnumerator();
    }

    protected override Type GetRowType() {
        return typeof(Employee);
    }
  }

  public class Employee : DataRow {
    internal Employee(DataRowBuilder builder)
      : base(builder) {
    }

    public Int64 Id {
      get { return (Int64)base["Id"]; }
      set { base["Id"] = value; }
    }

    public string FirstName {
      get { return (string)base["Firstname"]; }
      set { base["Firstname"] = value; }
    }

    public string Surname {
      get { return (string)base["Surname"]; }
      set { base["Surname"] = value; }
    }
  }
}
  • 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-12T06:34:32+00:00Added an answer on May 12, 2026 at 6:34 am

    I believe that I’ve answered my question, kind of. I’ve had to use .net 4.0 to get the results that I was hoping for specificially the dynamic type.

    So changing the existing class in the question:

    Employee.cs

    using System;
    using System.Data;
    using System.Collections;
    using System.Data.Common;
    
    namespace TypedDataSet {
    
      public class Employees : BaseModel<Employee> {
    
        public Employees(bool loadAll) {
            DbDataAdapter adapter = base.Adapter("SELECT * FROM Employees");
            adapter.Fill(this);
        }
    
        protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
            return new Employee(builder);
        }
      }
    
      public class Employee : DataRow {
        internal Employee(DataRowBuilder builder)
          : base(builder) {
        }
    
        public Int64 Id {
          get { return (Int64)base["Id"]; }
          set { base["Id"] = value; }
        }
    
        public string FirstName {
          get { return (string)base["Firstname"]; }
          set { base["Firstname"] = value; }
        }
    
        public string Surname {
          get { return (string)base["Surname"]; }
          set { base["Surname"] = value; }
        }
      }
    }
    

    And now introducing BaseModel that the above class inherits


    BaseModel.cs

    using System;
    using System.Data;
    using System.Collections;
    using System.Data.Common;
    using System.Data.SqlClient;
    
    namespace TypedDataSet {
    
        public class BaseModel<T> : DataTable {
            protected DbDataAdapter _adapter;
            protected string _connectionString = TypedDataSet.Properties.Settings.Default.ConnectionString;
    
            public BaseModel() {
            }
    
            protected DbDataAdapter Adapter(string sql) {
                _adapter = new System.Data.SqlClient.SqlDataAdapter(sql, _connectionString);
                SqlCommandBuilder cb = new SqlCommandBuilder((SqlDataAdapter)_adapter);
                return _adapter; 
            }
    
            public dynamic this[int index] {
                get { return Rows[index]; }
            }
    
            public void Add(dynamic row) {
                Rows.Add(row);
            }
    
            public void Remove(dynamic row) {
                Rows.Remove(row);
            }
    
            public void Save() {
                _adapter.Update(this);
                this.AcceptChanges();
            }
    
            public dynamic GetNewRow() {
                dynamic row = (dynamic)NewRow();
                return row;
            }
    
            public IEnumerator GetEnumerator() {
                return Rows.GetEnumerator();
            }
    
            protected override Type GetRowType() {
                return typeof(T);
            }
        }
    }
    

    Which allows me to consume the class using the following code:


    Employees employees = new Employees(true);
    
    Employee employee = employees.GetNewRow();
    employee.FirstName = "Greg";
    employee.Surname = "Focker";
    employees.Add(employee);
    
    employees.Save();
    
    foreach (Employee e in employees) {
      Console.WriteLine(e.FirstName + ' ' + e.Surname);
    }
    

    I hope to evolve this further overtime so future StackOverflow users if you’re interested in this little project take a look at http://bitbucket.org/Mozketo/typeddataset/ where I hope to host the code.

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

Sidebar

Ask A Question

Stats

  • Questions 168k
  • Answers 168k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer is XService.poll a 'function' and 'XService.pollInterval' a number at that… May 12, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer Attachments are encoded as multipart similar to multipart file uploads.… May 12, 2026 at 1:47 pm
  • Editorial Team
    Editorial Team added an answer From "301 Redirects and Search Engine Optimization": From a search… May 12, 2026 at 1:47 pm

Related Questions

I have recently come across a very simple Typed DataTable (without using a .XSD)(I've
I think back to Joel Spolsky's article about never rewriting code from scratch. To
I've just been experimenting with the boost::function_types library recently, and I've come across a
I've used some very large scale systems and never seen a required order, but
I have recently come across a situation where code is dynamically loading some libraries,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.