Is working with a database really this hard?

  • Thread starter Thread starter rick cameron
  • Start date Start date
R

rick cameron

....or am I just dense?

As a starter, I want to put a control on a Windows form that will display
the contents of a table in an Access database, and allow me to edit rows &
add new ones. It seems to me that this was a no-brainer in VB pre-.NET
('though I'm not a VB programmer).

I would have thought you could do this in C# without writing a line of code.

Any quick answers, or pointers? I've looked in MSDN Library & gotdotnet.com,
but haven't found a sample that doesn't involve writing a whack of obscure
code.

Cheers

- rick
 
Rick:

If you create a Strongly Typed dataset, then go to each control on the form
after you've dragged an instance of the dataset on the form. Set each
control's "Data Bindings" Text property to the corresponding field in the
dataset. Next, configure a dataAdapter by dragging it onto a form and
configure it so the SQL statemenet matches your dataset. When the form
loads, call DataAdapter.Fill (dataSetInstanceName).

At this point, your controls will be bound. You can then just call
DataAdapter.Update(datasetname.datatable) to submit the update. The only
thing you'll need to do is set up a binding context (look up BindingContext
on MSDN or Google) and navigation buttons. If you do according to a typical
example, you'll have one line of code to fill the data adapter, two lines to
set up the binding context(depending on the overload maybe 2), then one for
the Move Forward button, move back button, move end button and move
beginning button. So you are under 10 lines of code. Also, you can go to
Project, Add New Item, Data Form Wizard and it will walk you through it.

You'll need like two l.ines of code then.

However, using a Designer and not knowing what it does is dangerous and I
don't recommend it. In general, programming anything worthwhile in C# or
VB.NET is going to take some code and until you get a good grip of the
framework and your language of choice, your code will probably be
compartively verbose.

Check out either David Sceppa's ADo.NET core Reference or bill Vaugn's ADO &
ADO.NET best practices...they'll get you through everything you'll need.

Also, Check out www.knowdotnet.com and check out or dataacess section, we
have a ton of stuff on this subject. So does MSDN, C-SharpCorner,
codeproject etc.

HTH,

Bill
 
Hi, Bill

Thanks for the quick reply.

I agree that one should not rely too much on wizards - however, it's a great
way to get some working code written from which you can learn!

The Data Form wizard did just what I wanted. It made me realise what I
needed in my project - an Ole DB connection, an Ole DB data adapter, and a
data set; and I stumbled on the mini-wizard that configures the adapter, and
the mini-wizard in the Property sheet for the Ole DB adapter that
auto-generates the gnarly data set class.

The only remaining stumbling block was how to insert a new row in the table
_in_code_. I noticed something about the DataTable class having a NewRow
method, and figured I'd try using it. Sure enough: call NewRow, cast to the
custom row type the wizard generated, fill in the properties (named,
conveniently, the same as the columns in the table), add the row to the
DataTable, then tell the adapter to update using the DataTable. Woo-hoo - it
worked!

I feel like writing the article I haven't been able to find on any of the
websites - how to make your app able to load and save data to an Access
database...

Thanks for the pointers.

- rick
 
Hope you do write the article, Rick, and publish it in CodeProject or a similar high-quality .NET site.

best, Bill Woodruff
dotScience
Chiang Mai, Thailand
 
Hi Rick,

I am glad Bill's detail reply makes sense to you.

Do you still have any concern? If you need further help, please feel free
to post, we will help you. thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
rick said:
The only remaining stumbling block was how to insert a new row in the table
_in_code_. I noticed something about the DataTable class having a NewRow
method, and figured I'd try using it. Sure enough: call NewRow, cast to the
custom row type the wizard generated, fill in the properties (named,

If you've got a strongly-typed DataSet, then you also have
strongly-typed DataTables within that type, that have NewRow() methods
that return strongly-typed DataRows for you... you don't need to cast at
all.
 
Back
Top