.NET Platform has provided a lot of extra advantages to its developers comparatively than other platforms. It provides a rich set of tools, libraries etc that makes the work of developers a lot easier. One of those features is to help developers creating a desktop form based application very easily just by following couple of steps, without writing a single line of c# code, not even a single query. This is very much help full of the inventory based application for manipulating items information.I will provide here a step by step ways to create a simple database driven windows application that contain one record at a time and allow user to traverse through all of them via navigation system including code example. Here I am assuming that you have little familiar with visual studio, c# and Microsoft SQL Server.
Lets Develop A Database Driven Windows Application:
- We will first have to create a project from File->New->Project option. Select Windows Forms Application(VS2008+)/Windows Application(VS 2005).
- From Data->Add New Data source, make a new connection to your desired SQL server database.
- Again from Data->View Data Source, browse your database. Now go to your specific table. Drag the specific columns those you want to be shown on the application’s user interface form. Remember here, visual studio by defaults sets an proper control that can be associated with the table column, like for type ‘varchar’ or ‘text’, a text box control will be associated. For type ‘bit’, a check box will be associated, etc. You can always change it from your server explorer window, using drop down that is just right before the column.
- Now from View->Toolbox option, select ‘Data’ category, drag a “bindingNavigator” object to the form. You will see this includes next/previous/add/delete etc options on it to do the operations on database records.(Actually, you will may not have to do this also, this should be automatically imported while adding the fields to the form)
- Now, you will notice, on design view , several objects like dataset, tableadapter, bindingsource, bindingnavigator etc have been created in the design tray. select the binding navigator object, right-click, view properties, please set the ‘BindingSource’ property to your bindingsource object.
Yahooooo! We have done the basic implementation without even a single line of code! It’s amazing, isn’t it? Well we will need a bit for more functionality. Keep reading. This drag-drop facility of .NET/visual studio has made our lives easier and have relived us from writing boring same code again and again.
Now Notice That, If you click add, it will give you an empty form to fill up, you can edit the filed data also. However, there is no save button to save the data. This is the section we are going into real coding, as right before now, we didn’t have to write a single line of code at all.
Now, simple add a button, either in the navigator toolbox or anywhere in the form, which suits you, doesn’t matter where. go to the button’s click event.
this.Validate(); this.myBindingSource.EndEdit(); myTableAdapter.Update(myDataSet);
This will work out for all kind of events, such as delete, add or update. Lets know what this sample codes do.(This step also may not required at all as i experienced while working on visual studio 2010, just for reference if you need to)
this.Validate() validates the value of the controls that is loosing focus if automatic validation is turned on. this.myBindingSource.EndEdit(), its send the changes to the data source(not the database yet) which stored on volatile memory. myTableAdapter.Update(myDataSet), this is the actual method, that is responsible to send changes to database. Here you can update a whole dataset or only a table or even only one or couple of rows, it has several overloaded method for each type.
For Practice I have uploaded a sample project that is done on visual studio 2005 so that, you can open/upgrade it to your version(2005/2008/2010).
Don’t like to work with winform style? Looking for more advanced desktop UI? What about learning WPF application development? It will give you a nicer layout maker in XML format. Also, if you also work on web applications, then working with WPF will seem to be more familiar to you as because of its xml coded design and stylesheet applying strategy. Give it a try!
Hope, this simple tutorial on database driven windows application development with C# will help you in the way to create large-scale project with reduced development time, where many developer still using manual ways by drag drop form controls, then by using their own button to save/insert,delete with their own implementation, where they often make several mistakes and resulting in a more time spent for bug fixing etc. There are also a lot other features on visual studio/.NET , which i will try to describe on this blog time to time. Happy coding