0

The other day I started working on a project in C#.NET using Windows Forms and it's been quite a while since I last used Windows Forms so I'm feeling a bit rusty.

First off I should point out that I'm using Visual C# 2010 Express which does not have GUI wizard support for databindings (well it does, if you enjoy MS Access or locally stored database files) and I need to speak to a MySQL database and make it play nice with a DataGridView.

My problem stems from the fact that I can't quite figure out how to do updates and deletes. Most online sources I've found seem to just refer to running the data binding wizard, something I can't do (since I'm running Visual C# 2010 Express).

Now, I've been able to select data from the database without any major problems (using a MySqlDataAdapter, a BindingSource and a DataTable) but when I try to use MySqlDataAdapter.Update(<DataTable>) nothing happens.

I'm assuming there is some setup work I need to do which is normally handwaved away as "just use the wizard". So, does anyone have a simple example of what I need to do to be able to do updates, inserts and deletes? With or without stored procedures, both ways are fine (although I'll probably end up used stored procs in the finished program).

mludd
  • 729
  • 2
  • 7
  • 23
  • These links may be helpful: http://stackoverflow.com/questions/1518946/how-to-insert-delete-select-update-values-in-datagridview-in-c-using-mysql http://stackoverflow.com/questions/4663632/c-insert-and-update-content-to-datagridview-do-not-work http://stackoverflow.com/questions/832874/sqldataadapter-update-doesnt-work – Mamta D Sep 29 '11 at 09:57
  • I've looked at both those and tried for several hours to make my code behave, it doesn't. The annoying part is that there are no exceptions or such, it just doesn't do anything. So yes, I've tried setting up an `UpdateCommand` and I've tried calling `EndEdit()` and I've tried both, it just isn't working. But selects are working just fine though... – mludd Sep 29 '11 at 10:49

1 Answers1

2

I suggest you to do not use those DragDroppable DataAdapter or BindingSources from the VS Toolbox, just layer your project properly in different projects each one with its own concern, like Data Access, Business Logic and Presentation (GUI).

see my answer here: MVC3 and Entity Framework

if you have a solid layered / m-tier architecture you will find out that data management logic belongs only to the Data Access Layer and the UI just has to ask what is needed going through the Business Logic.

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • +1 - as stated above. Just becuase you're using Windows forms doesn't mean that you should forget the basics of decent architecture. You can tie your datagrid to any collection type - so a List will suffice to populate the datagrid. You can then write logic in the data layer to perform the updates to the database etc. – ChrisBD Sep 29 '11 at 12:56
  • Thanks, this is actually what I sort of ended up doing, I wrote a bunch of my own code. It may not match MS' idea of best practices but it worked... – mludd Sep 29 '11 at 14:16