VB.Net Login Code with MySQL Database
This tutorial is all about VB.Net Login Code with MySQL Database. In this tutorial, I’m going to show you how to create a simple Login-Logout system using Visual basic.net and MySQL Database.
To start with this VB.Net Login Code with MySQL Database the given steps below.
But if you want to start how to code using visual studio 2019, you can start your lesson here on how to connect Mysql to visual Studio 2019.
VB.Net Login Code with MySQL database
- Step 1: Open Visual Basic
You have many options for what version of visual studio you’re going to use. But make sure to use at least the minimum version of visual studio 2008.
Note: The vb login code presented in this tutorial can be used in a higher version of VB such as 2010, 2013, 2017, and the likes. - Step 2: Create a Project
For creating a project you see the image presented below.
- Step 3: Select a Project
Under New Project, Choose Windows then Select Windows Form Application, and Save it as “vbmysql”.
- Step 4: Design the Object Properties
This time, let’s add objects to our windows form and these objects are the following: four Labels, two Textbox, two buttons, and a Groupbox. (See the table below for the Object Design Properties)
Object Design Properties
Object | Property | Settings |
Label1 | Name | lbllogin |
Text | Login | |
Label2 | Name | lblname |
Text | Hi, Guest! | |
Label3 | Text | Username |
Label4 | Text | Password |
Textbox1 | Name | txtuname |
Textbox2 | Name | txtpass |
PasswordChar | * | |
Button1 | Name | btnok |
Text | OK | |
Button2 | Name | btncancel |
Text | Cancel |
Step 5: After setting the Object properties, arrange all the objects like as shown below.
Step 6: This time, let’s perform the VB.Net Login Code with MySQL Database.
VB.Net Login Code with MySQL Database in Action
Double click the form and add the following code:
This code will simply disable the group box that holding the label Username and Password same with the two textbox and two Buttons.
GroupBox1.Enabled = False
Step 7: Next, double click the “lbllogin” label and add the following code:
This code will check if the “lbllogin” is set to “Logout” then it reset the text of “lbllogin” to “Login” same with the “lblname” to “Hi, Guest!”, else if the text of “lbllogin” is equal to “Login” then it’s enabled the Group box.
If lbllogin.Text = "Logout" Then
lbllogin.Text = "Login"
lblname.Text = "Hi, Guest!"
ElseIf lbllogin.Text = "Login" Then
GroupBox1.Enabled = True
End If
8. Step 8: Then add the following code under the public class.
'Represents an SQL statement or stored procedure to execute against a data source.
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
'declare conn as connection and it will now a new connection because
'it is equal to Getconnection Function
Dim con As MySqlConnection = jokenconn()
Public Function jokenconn() As MySqlConnection
Return New MySqlConnection("server=localhost;user id=root;password=;database=studentdb")
End Function
Step 9: And double click the “OK” button and add the following code:
Dim sql As String
Dim publictable As New DataTable
Try
'check if the textbox is equal to nothing then it will display the message below!.
If txtuname.Text = "" And txtpass.Text = "" Then
MsgBox("Password or Username Incorrect!")
Else
sql = "select * from tbluseraccounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
'bind the connection and query
With cmd
.Connection = con
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(publictable)
'check if theres a result by getting the count number of rows
If publictable.Rows.Count > 0 Then
'it gets the data from specific column and assign to the variable
Dim user_type, name As String
user_type = publictable.Rows(0).Item(4)
name = publictable.Rows(0).Item(2)
'check if the type of user is admin
If user_type = "Admin" Then
'welcomes the user as Admiistrator
MsgBox("Welcome " & name & " you login as Administrator ")
'set the lbllogin text to Logout
lbllogin.Text = "Logout"
'disabled the groupbox
GroupBox1.Enabled = False
'reset all the two textbox
txtuname.Text = ""
txtpass.Text = ""
'set the lblname to the specific user
lblname.Text = "Hi, " & name
ElseIf user_type = "Encoder" Then
MsgBox("Welcome " & name & " you login as Encoder ")
lbllogin.Text = "Logout"
GroupBox1.Enabled = False
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
Else
MsgBox("You login as Guest!")
lbllogin.Text = "Logout"
GroupBox1.Enabled = False
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
End If
Else
MsgBox("Contact administrator to registered!")
txtuname.Text = ""
txtpass.Text = ""
End If
da.Dispose()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Clone()
Step 10: This time you can now test your application by pressing “F5”.
After testing the program, here’s all the following Login Code with MySQL Database used in this application.
Imports MySql.Data.MySqlClient
Public Class Form1
'Represents an SQL statement or stored procedure to execute against a data source.
Dim cmd As New MySqlCommand
Dim da As New MySqlDataAdapter
'declare conn as connection and it will now a new connection because
'it is equal to Getconnection Function
Dim con As MySqlConnection = jokenconn()
Public Function jokenconn() As MySqlConnection
Return New MySqlConnection("server=localhost;user id=root;password=;database=studentdb")
End Function
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
Dim sql As String
Dim publictable As New DataTable
Try
'check if the textbox is equal to nothing then it will display the message below!.
If txtuname.Text = "" And txtpass.Text = "" Then
MsgBox("Password or Username Incorrect!")
Else
sql = "select * from tbluseraccounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
'bind the connection and query
With cmd
.Connection = con
.CommandText = sql
End With
da.SelectCommand = cmd
da.Fill(publictable)
'check if theres a result by getting the count number of rows
If publictable.Rows.Count > 0 Then
'it gets the data from specific column and assign to the variable
Dim user_type, name As String
user_type = publictable.Rows(0).Item(4)
name = publictable.Rows(0).Item(2)
'check if the type of user is admin
If user_type = "Admin" Then
'welcomes the user as Admiistrator
MsgBox("Welcome " & name & " you login as Administrator ")
'set the lbllogin text to Logout
lbllogin.Text = "Logout"
'disabled the groupbox
GroupBox1.Enabled = False
'reset all the two textbox
txtuname.Text = ""
txtpass.Text = ""
'set the lblname to the specific user
lblname.Text = "Hi, " & name
ElseIf user_type = "Encoder" Then
MsgBox("Welcome " & name & " you login as Encoder ")
lbllogin.Text = "Logout"
GroupBox1.Enabled = False
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
Else
MsgBox("You login as Guest!")
lbllogin.Text = "Logout"
GroupBox1.Enabled = False
txtuname.Text = ""
txtpass.Text = ""
lblname.Text = "Hi, " & name
End If
Else
MsgBox("Contact administrator to registered!")
txtuname.Text = ""
txtpass.Text = ""
End If
da.Dispose()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Clone()
End Sub
Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click
If lbllogin.Text = "Logout" Then
lbllogin.Text = "Login"
lblname.Text = "Hi, Guest!"
ElseIf lbllogin.Text = "Login" Then
GroupBox1.Enabled = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.Enabled = False
End Sub
End Class
Conclusion
This Login Code with MySQL Database will only help you to add security in your software application.
You can only enjoy the vb.net programming if you take the time to practice the knowledge you get from this material.
I hope this tutorial would really help you to improve your knowledge of programming.
Note: If you have any questions or suggestions about VB.Net Login Code with MySQL Database, please feel free to contact us or simply leave a comment below.
Info analysis is a dedication to learning. This wants specific analysis expertise, an consciousness of research instruments and gifted mind. Without knowledge of and access to relevant research worthy resources, your search will probably be severely limited and uncertain. Web is the one huge area of exploring which provide us the source of research instruments. If we give a reputation explorer to Web that would not be wrong, in a sense that explorer itself means to inquire one thing or to look at something. Internet is widely used within the area where there is a work of analysis. Internet just isn’t decrease to this field solely, it also present us the perfect source of leisure.
Hello! This post couldn’t be written any better! Reading this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Fairly certain he will have a good read. Thank you for sharing!
The New Chronicle newspaper was bought by those with plenty money. For them $$$$$ ,no downside. That transfer was by no means to offer more timely, factual, objective info on the problems.
I’ll right away snatch your rss as I can’t find your email subscription link or e-newsletter service. Do you’ve any? Kindly let me recognise in order that I could subscribe. Thanks.
I do believe all the concepts you’ve presented on your post. They are really convincing and can definitely work. Nonetheless, the posts are very brief for starters. Could you please prolong them a little from next time? Thank you for the post.
I blog frequently and I seriously appreciate your content. This great article has really peaked my interest. I’m going to book mark your blog and keep checking for new information about once a week. I subscribed to your Feed too.
An outstanding share! I’ve just forwarded this onto a friend who was conducting a little research on this. And he actually bought me breakfast due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending some time to talk about this subject here on your web page.
Fine way of telling, and pleasant paragraph to take information regarding my presentation topic, which i am going to convey in college.
It’s actually a great and helpful piece of info. I am satisfied that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.
Simply desire to say your article is as astonishing. The clarity in your post is simply cool and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please continue the gratifying work.
Wow! In the end I got a weblog from where I know how to genuinely get useful facts concerning my study and knowledge.
Since the admin of this website is working, no question very soon it will be well-known, due to its quality contents.
Excellent post. I am facing some of these issues as well..
That is really interesting, You’re a very professional blogger. I’ve joined your feed and look forward to searching for extra of your great post. Also, I’ve shared your web site in my social networks
Hi, i feel that i noticed you visited my site thus i got here to go back the desire?.I’m attempting to to find issues to enhance my website!I suppose its ok to use some of your ideas!!
I just like the helpful info you supply to your articles. I’ll bookmark your weblog and test once more here regularly. I’m somewhat sure I will be told many new stuff right right here! Best of luck for the following!
I’m really loving the theme/design of your website. Do you ever run into any internet browser compatibility issues? A small number of my blog audience have complained about my website not operating correctly in Explorer but looks great in Chrome. Do you have any ideas to help fix this issue?
Hey there, You have done a fantastic job. I’ll definitely digg it and personally recommend to my friends. I am confident they will be benefited from this web site.
Thanks for the marvelous posting! I quite enjoyed reading it, you may be a great author.I will be sure to bookmark your blog and will often come back down the road. I want to encourage you continue your great writing, have a nice day!
I am not sure where you’re getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for fantastic information I was looking for this information for my mission.
Hi, after reading this amazing paragraph i am also happy to share my knowledge here with colleagues.
Currently it seems like WordPress is the preferred blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?
I do not even know how I ended up right here, however I thought this post was great. I don’t know who you are but definitely you’re going to a well-known blogger when you aren’t already. Cheers!
I’m gone to inform my little brother, that he should also visit this web site on regular basis to take updated from latest news.
I am regular visitor, how are you everybody? This post posted at this site is truly nice.
You’re so awesome! I don’t think I have read anything like this before. So good to find another person with a few unique thoughts on this subject matter. Really.. thanks for starting this up. This web site is one thing that is needed on the web, someone with a little originality!
It is normally fairly straightforward to seek out somewhere even for an expat.
Instead they’ve deferred the choice to the courts.
I’d like to find out more? I’d care to find out more details.
My family every time say that I am wasting my time here at net, except I know I am getting knowledge every day by reading such pleasant articles.
Very good information. Lucky me I recently found your blog by chance (stumbleupon). I’ve saved as a favorite for later!
My spouse and I absolutely love your blog and find a lot of your post’s to be exactly I’m looking for. Would you offer guest writers to write content to suit your needs? I wouldn’t mind publishing a post or elaborating on a few of the subjects you write in relation to here. Again, awesome blog!
Howdy would you mind letting me know which web host you’re working with? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you recommend a good internet hosting provider at a reasonable price? Many thanks, I appreciate it!
Hello there! This post couldn’t be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this write-up to him. Fairly certain he will have a good read. Many thanks for sharing!
I do agree with all of the ideas you have presented for your post. They’re really convincing and will definitely work. Nonetheless, the posts are very short for newbies. May just you please lengthen them a little from subsequent time? Thank you for the post.
This paragraph offers clear idea in favor of the new visitors of blogging, that truly how to do blogging and site-building.
We’re a bunch of volunteers and starting a brand new scheme in our community. Your web site offered us with helpful info to work on. You have done an impressive job and our whole group shall be thankful to you.
I’ve been surfing online more than three hours as of late, yet I never found any fascinating article like yours. It is pretty price enough for me. In my view, if all website owners and bloggers made just right content as you probably did, the web can be much more helpful than ever before.
Pretty section of content. I simply stumbled upon your website and in accession capital to say that I get in fact loved account your weblog posts. Any way I’ll be subscribing for your feeds and even I success you get entry to persistently quickly.
Hi to all, the contents present at this website are actually remarkable for people experience, well, keep up the nice work fellows.
obviously like your web site but you have to test the spelling on quite a few of your posts. Several of them are rife with spelling problems and I find it very troublesome to inform the truth on the other hand I’ll certainly come back again.
I blog quite often and I really thank you for your information. Your article has truly peaked my interest. I’m going to take a note of your site and keep checking for new information about once a week. I subscribed to your RSS feed as well.
I will immediately clutch your rss feed as I can’t to find your e-mail subscription link or newsletter service. Do you have any? Kindly let me know in order that I could subscribe. Thanks.
Hi, I think your website might be having browser compatibility issues. When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, great blog!
Nice post. I used to be checking constantly this weblog and I’m inspired! Very helpful information specifically the last phase 🙂 I maintain such info much. I was seeking this particular information for a very lengthy time. Thank you and good luck.
It’s going to be end of mine day, however before ending I am reading this great article to improve my knowledge.
I delight in, cause I discovered exactly what I was having a look for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye
Hello to all, the contents present at this site are in fact amazing for people experience, well, keep up the nice work fellows.
Came back when I calmed down.
Excellent goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still care for to keep it sensible. I can’t wait to read far more from you. This is actually a tremendous site.
whoah this weblog is fantastic i really like studying your articles. Keep up the good work! You already know, a lot of individuals are searching around for this info, you can aid them greatly.
Hello I am so glad I found your site, I really found you by mistake, while I was looking on Bing for something else, Regardless I am here now and would just like to say cheers for a remarkable post and a all round thrilling blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the excellent b.
I am really enjoying the theme/design of your weblog. Do you ever run into any browser compatibility problems? A small number of my blog readers have complained about my website not operating correctly in Explorer but looks great in Safari. Do you have any advice to help fix this issue?
I have read so many articles regarding the blogger lovers however this piece of writing is genuinely a good paragraph, keep it up.
I was excited to discover this site. I wanted to thank you for ones time for this fantastic read!! I definitely really liked every little bit of it and I have you bookmarked to see new stuff on your blog.
Very shortly this website will be famous among all blogging viewers, due to it’s nice posts
Awesome post.
Hey, I think your site might be having browser compatibility issues. When I look at your website in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, terrific blog!
I think this is one of the most vital info for me. And i’m glad reading your article. But want to remark on few general things, The website style is great, the articles is really excellent : D. Good job, cheers
I think this is among the most important information for me. And i’m happy reading your article. But should commentary on some general things, The website taste is ideal, the articles is truly nice : D. Excellent activity, cheers
Hey there, I think your website might be having browser compatibility issues. When I look at your website in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, wonderful blog!
I am regular visitor, how are you everybody? This post posted at this site is in fact good.
Thanks a bunch for sharing this with all folks you really recognize what you’re talking about! Bookmarked. Kindly also visit my website =). We will have a hyperlink change contract among us
continuously i used to read smaller content that also clear their motive, and that is also happening with this paragraph which I am reading now.
It’s an awesome piece of writing designed for all the internet viewers; they will take benefit from it I am sure.
Im grateful for the article post.Really thank you! Really Cool.