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!
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):
Then from your page or other class do something like:
Optionally if you wanted to create a new user:
And saving any new info is fairly easy too:
…