Updating Database Table with Programming in VB 2010

Hello friends, I hope you all are fine and having fun. In today's tutorial, we will have a look at updating database table with Programming in VB 2010. In the previous posts of this tutorial, we have seen How to Create a database in VB 2010 and then we have added button control in VB Database in it, which was quite an easy job. But usually in such applications, its necessary that the application update its database automatically. Like in one of my project, I have designed a software for hotel management in which the data is coming from all the rooms wirelessly to the master room where the software is installed. It was quite a big project but the thing, for which I have mentioned this project, is the software installed in the master room was updating its database itself i.e. if data of room 1 changed then software automatically update the info of that room in its database.

So, for students and also for freelancers its essential that they must have full command over database, and in order to understand today's post its also essential that you must read the previous two posts of this tutorial.
  • In the last post of this tutorial, we have designed this application which is shown in the below figure:
  • In this post I am not gonna use images as its really not necessary, because we don't make any change in the layout of application, the whole programming will be at the back end.

Code for Updating Database internally in VB 2010

  • Here's the simple code which you need to place in your application, I have explained each line with comments but still if anyone got any trouble please ask in comments, I am always here to help you out.
  • For this code, things to be noted are:
    • database name is database1,  
    • Var1 and Var2 are two variables,  
    • Contact Form is the name of our table which we have saved in our database.
    • FullName and LastName are the columns name of our table.
Dim Var1 As database1DataSet.ContactFormRow 'Defining a vaiable Dim Var2 As database1DataSet.ContactFormRow 'Defining a variable Var1 = database1DataSet.ContactForm.FindByID(2) 'Here, we are assigning our variable Var1 the location of row 2. Var2 = database1DataSet.ContactForm.FindByID(2) 'Here, we are assigning our variable Var2 the location of row 2. Var1.FullName = Var2.LastName 'Now as our variables are in row 2 so they can access any value in row 2. So in this line of code, I just assign the LastName to First Name. So, after execution of this command whatever present in Full Name of row 2 will be replaced by Last Name of row 2. Me.table5TableAdapter.Update(Me.database1DataSet.ContactForm) 'Now at the the end updated our database or in other words save the database.
Mostly data we receive is through serial port so in that case simple remove the Var2 with the variable in which you are receiving data from serial port. For the programming of serial port in visual basic 2010, check the below tutorial. That's all for today friends and finally this tutorial completed. If you guys have any question regarding any part kindly ask in comments and also subscribe to our newsletter to get the new updates and software.
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir