• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Database / Database-Driven Desktop Form Tutorial Using C#.NET

Database-Driven Desktop Form Tutorial Using C#.NET

November 3, 2010 by Rana Ahsan 9 Comments

C#.NET Tutorials

.NET Platform has provided a lot of extra advantages to its developers compared to 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 create a desktop form-based application very easily just by following a 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 step-by-step ways to create a simple database-driven windows application that contains one record at a time and allow the user to traverse through all of them via a navigation system, including code example. Here I am assuming that you have a little familiarity with visual studio, c# and Microsoft SQL Server.

Let’s Develop A Database Driven Windows Application:

  1. We will first have to create a project from File->New->Project option. Select Windows Forms Application(VS2008+)/Windows Application(VS 2005).
    Creating New Windows Form Project
  2. From Data->Add New Data source, make a new connection to your desired SQL server database.
    Adding new data source
  3. Again from Data->View Data Source, browse your database. Now go to your specific table. Drag the specific columns that you want to be shown on the application’s user interface form. Remember here, visual studio, by default, sets a proper control that can be associated with the table column; 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 the drop-down that is just right before the column.
    Adding database table field to desktop form application window
  4. Now from the View->Toolbox option, select the ‘Data’ category, and 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 not have to do this. Also, this should be automatically imported while adding the fields to the form)
  5. Now, you will notice, on the design view, several objects like dataset, tableadapter, bindingsource, bindingnavigator etc have been created in the design tray. select the binding navigator object, right-click, and view properties, please set the ‘BindingSource’ property to your bindingsource object.
C#.NET Desktop Form Application

We are done!
Build your application, run it, and you will see the records of your database table and be able to navigate through each record using the default buttons.

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, simply 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);
Code language: CSS (css)

This will work out for all kinds of events, such as delete, add or update. Let’s know what these sample codes do.(This step also may not require 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 are losing focus if automatic validation is turned on. this.myBindingSource.EndEdit(), sends the changes to the data source(not the database yet), which is stored on volatile memory. myTableAdapter.Update(myDataSet), this is the actual method that is responsible for sending changes to the database. Here you can update a whole dataset or only a table, or even only one or couple of rows; it has several overloaded methods 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).

Download Sample Project

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!

I hope this simple tutorial on database driven windows application development with C# will help you in the way to create a large-scale project with reduced development time; where many developers still use manual ways by drag-drop form controls, then using their own buttons 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 of other features on visual studio/.NET, which I will try to describe in this blog from time to time. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Database Tagged With: .net, c#

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. Akshay Darekar says

    December 17, 2011 at 10:55 pm

    OK, I am learning on my own…

    Hey this allows us to add data to the database, but my question is how to retrieve the data from a database?

    lets say, you select roll no from ID (can we add drop-down here?), and this pops his name in ‘name’ field… How do you do that?

    I have been able to update my database using this work though…

    Reply
    • Rana says

      December 18, 2011 at 1:43 am

      Hi,
      Here I showed a very simple way to build a form app. You can customize it in whichever way you want. In your case, you sure can add a combo box, then bind it with list of rolls, retrieved from db. On the combo box’s event properties, add event handler on ‘seletectedindexcommited’ event. And then retrieve the coresponding row. However, you will need to get help of some coding here, for sure , 🙂

      Reply
  2. Krunal says

    October 11, 2012 at 2:01 am

    database is not secure…..
    tell me any secure database …….

    Reply
  3. harini says

    January 22, 2013 at 12:45 am

    hi, the coding was more useful its working correctly

    Reply
  4. harini says

    January 22, 2013 at 1:09 am

    “Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.”
    this error i got when i clicked X button in binding navigator… pls help

    Reply
    • Md. Ali Ahsan Rana says

      January 25, 2013 at 5:42 am

      Hi Harini, sorry for delayed reply. I think, you haven’t set any primary key in your database table. Please do so and your issue should get resolved. Thanks.

      Reply
  5. Andrew Young says

    February 5, 2013 at 3:51 am

    Hello,

    I was thinking of making a (portable) database driven desktop application that can simply be thrown on a usb flash drive and run anywhere.

    Question 1: Is this possible with the microsoft.net platform or will I have to resort to other programming languages such as java?

    Question 2: Can your data source (database) be a ms access database?
    Thanks

    Reply
  6. md imran ali says

    March 21, 2013 at 10:11 pm

    really nice

    Reply
  7. Ame says

    September 17, 2015 at 6:07 am

    how do you navigate C# SQL database wit NEXT and PREVIOUS buttons in webApplications asp.net

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Getting Started With Smarty Template Engine
    Getting Started With Smarty Template Engine
  • Pipe Email To PHP And Parse Content
    Pipe Email To PHP And Parse Content
  • Building Auth With JWT – Part 2
    Building Auth With JWT – Part 2
  • Getting Started With HTML5 Web Speech API
    Getting Started With HTML5 Web Speech API

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023