MySQL VB.NET Tutorial 6 : Insert/Save data to database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome once again in the last few videos I have been showing you how to create a login page using your visual basic windows form application and MySQL database so till now I have shown you how to check connection with the database and it shows the message successful connection if our Visual Basic project is connected to the database and validate user name and password with the database as username and password and when I click login button and if the username and password matches the username and password in the database it opens the second form now in this video I am going to show you how to save your data in the database using these text boxes and this button so suppose you want to add some data in the database using your Visual Basic windows form application in this video I will show you exactly that so let's get started so in the last video itself I have shown you how to create the second form right and I have also shown you how to create this sign out button so that when you click the Sign Out button it goes to the first form now in this video just you need to add these things so what you need to do is add some labels and suppose I have this table in the database right I have the database and I have the table in the database called II data and I have this column e ID name surname and H another username password gender date of birth and other things suppose I want to enter some new data for a ID name surname and age so till now I have 14 entries in the database till 9:14 I want to add employee ID 15 using my visual basic windows form application so what I you need to do first is take some labels to write your column name data so I'm adding a ID name surname and age which is there which are already there in the database these are the columns EW I name surname and age and add some text boxes so that we can enter the values which we want to save in the database and I have already changed the name of these text boxes according to their use so the first X boxes is text box underscore e ID for employee ID second text box is for text box name third text box name is textbox under for s name which is surname and fourth text box name is text box underscore age which is for age and just take a button this button when we click this button it should save the data in the database right so that is for saving the database now just double click this button and in here we are going to write the code to save the data in the database but first as we have imported this MySQL data dot MySQL client we need to once again import this to our second form so just add this line or add this import to the second form import MySQL the data dog MySQL client and we need to declare these two variables so I will just copy it and explain them at the top of your class so after the Declaration of public class form 2 you can declare these variables Y first variable is them MySQL con as my skill connection which is the connection to the database which will accomplish the connection to the database and this is dim command as my skill command which will help you to execute a query to the database ok so just add these two lines as you have done in the form 1 if you don't know how I have added these things and how I've used these things you can watch my last videos and you will be able to understand what I have done here now once again in this button click event I want to write the code so what I am going to do is I'm going to copy the code which I have written in the login button action perform this is the code I have shown you when I have shown you how to create a login page right so I'm going to copy all the data and because most of the things would be repeated in saving the daughter like the connection and the command so I will copy this and I will show you step by step what to change in this code right so first of all I will make it little simple I am going to delete all this commands and I will delete this end while loop and now I will go to through the code and I will show you how to write the code so in this button click event just create a new instance of your MySQL connection so this is the variable which we have declared MySQL connection and in here you are creating the new instance of MySQL connection so just write your MySQL connection which is this variable is equal to new MySQL connection right and then just copy this bicycle corn here dot connection string is equal to the connection to the server which is your MySQL server this line I have already described you number of times in the last video this line is used to create the connection with your Maskell so now declare a variable called dim reader as my SQL data reader this will read the data from the database so declare this here and in the try-catch block so just write a try and a catch block so just write this Calton here which what basically do is if any exception occurs while executing your query it catches the exception and shows the message and once the query is executed it disposes the connection so that you are ready to use it for the another connection so just write this code and this try to catch the exception so if you can copy in so you can pause the video and copy this code and this try block from here now and the tribe love what we are going what we are doing is we are opening the connection with the database so I'm just hoping this my school corn dot open this opens the connection to the database and then I declare a string variables which will hold my query so I declare them query as string and I will copy this query here is equal to I will write a new query in order to insert the data into the database so I will delete my old and I will write a new query here and and say insert into dot abased dot e dot Da so this database is the name of my database which is this database and edata is my table names database dot e dot right insert into database dot e data and then I will provide the column names in which I want to add my daughter first column name is Eid second column name is name third column name is surname and fourth column name is age right so if you see my column names PID name surname and age and if you are wondering why I have written all these things in small letters when you are passing SQL query it doesn't matter if you write them in capital letters or small letters it is not case-sensitive so your query to the Maya spell database is not case-sensitive so you can write this e ID as capital also and small loss okay so my query till now is insert into database dot e dot and in the bracket I have enter the name of the column e ID name surname and age then I will write values and in the bracket I will pass the values which I will enter in the text box so first value will be the name of the Eid textbox so first of all just give single quote and in between just give double quote and in between just give this and sign and in between you need to give the name of your text box so just write text box underscore PID which is the name of my text box right so text box understood PID okay in a fake in the same way I will enter name surname and eh also so we give former and I will copy and paste sorry I will copy and paste this value once again here and comma copy and paste once again and comma V and paste once again and I need to just change the value of these text boxes so this is text box name and I forgot to give dot txt here so just write textbooks name then by dot txt for T take the text from the ducklings and here also in the text box for Eid you need to give the name of the text box dot text right in the third field is my textbox named for the surname dot text and in the fourth field textbox name is Paige still textbox named JH dot text okay so now my query is complete so I will go through the query once again just right insert into data dot key data which is the table name and then in the bracket just write the name of the column in which you want to enter the data or save the letters of Eid name surname and age which are the name of my column in my II data table then write values so I had some spelling mistakes I have corrected values and in the bracket just write the name of your text box not text in this format white comma the name of the second text box name which is text box name dot txt comma the third text box name which is textbook for name dot txt comma the name of your fourth text box name which is text box each dot txt right so this is if ready then just write command is equal to MU my SQL command which is this command where you which we have declared above right dem command as my skin c'mon so just write command is equal to new my screen come on and in the bracket we passed two arguments first is the query which we have written here this one copy and paste it here and second is the connection with the database you just copy this connection from here and paste it here then we declare a reader just copy this reader variable from here is equal to come on dot execute right and discount you don't need so I deleted and now you can show the message that your data data is saved into the database so I will say message box dot show and I will say data saved okay so if your data is saved then this message will be shown and if some exception occurs in saving the database it will catch the exception here and if you show the error message from here okay so this is the code to save the data in the database so if you want you can pause the video and just copy this code from okay now I'm going to run my program blow to my first form which is login form I provide my username and password which is correct and opens the second form now I will open my database which is here until now I have employee ID 14 so I have 14 employee IDs already there now I want to enter the fifteenth employee ID so I will add employ ID 15 name some name and surname and E and when I click this Save button we see the message data saved that means your data is saved into the not of a success fee not to check this data what you need to do is booboo your table and you need to refresh your table so just take this refresh button and now you can see whatever you do data you have entered here 15 mark and the surname Hines and 8:34 is entered into the daughters now for example I am going to enter or ads or save up some more dot up employee ID sixteen you name is some name and surname is some surname and age is for example 45 and I say save and database that I said if you see this message status eight and once again i refresh my database and you can see here employed in sixteen and name is Julie and surname is Peters and age is 45 so in this way you can save the data into the database using your visual basic windows form application and MySQL database I hope you have enjoyed this video
Info
Channel: ProgrammingKnowledge
Views: 204,925
Rating: 4.7606835 out of 5
Keywords: Visual Basic .NET (Programming Language), Visual Basic (Programming Language), MySQL (Software), .NET Framework (Programming Language), Management, Technology, Tutorial, Data, Login, Software (Musical Album), Second, First, signout
Id: 3lGLxo1jXjg
Channel Id: undefined
Length: 18min 38sec (1118 seconds)
Published: Mon Mar 17 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.