VB Dot NET Forum: MySQL & VB.NET: Login form - VB Dot NET Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • This topic is locked

MySQL & VB.NET: Login form

#1 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 07 November 2007 - 03:14 PM

lately I have been learning how to use mysql with vb.net and if your interested in that as i am, you know theres only one really decent source for help out there and sadly even that location doesn't offer everything.

so i have taken it to myself to try to help out by posting some example code as i go along.

first code I want to post is with working with the login form.

to create a login form window, go to the solution explorer screen and right-click on the bolded text and go to add-> New item.

look for login form and select it.

then double click on the any of the buttons there, both by default just close out the program, you can keep the default value of that set for cancel unles you have other plans.

before we get too far, we need to import some values at the very top so that the program knows what we're doing:

Imports MySql.Data.MySqlClient
Imports System.Data


in the ok action remove the me.close code as we wont need it.

then we need to connect to the db, we also need to make sure the connection went through.

		Dim conn As MySqlConnection
		'connect to DB
		conn = New MySqlConnection()
		conn.ConnectionString = "server=localhost; user id=myroot; password=mypwd; database=login"
		'see if connection failed.
		Try
			conn.Open()
		Catch myerror As MySqlException
			MessageBox.Show("Error Connecting to Database: " & myerror.Message)
		End Try


pretty easy, now lets get ready to query the server.

'sql query
		Dim myAdapter As New MySqlDataAdapter

		Dim sqlquery = "SELECT username, password FROM userlist Where username='" & UsernameTextBox.Text & "' and password='" & PasswordTextBox.Text & "'"
		Dim myCommand As New MySqlCommand()
		myCommand.Connection = conn
		myCommand.CommandText = sqlquery
		'start query
		myAdapter.SelectCommand = myCommand
		Dim myData As MySqlDataReader
		myData = myCommand.ExecuteReader()


here we created a variable tha will handle the command we need to query the db and then made another variable to read the data.

now we have to check to ensure the user entered the correct login info.

		'see if user exits.
		If myData.HasRows = 0 Then
			MessageBox.Show("Invalid Login Details", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Else
			Dim frm1 = New Form1
			Frm1.Show()
			Me.Visible = False
		End If


here the if statement sees if the entered data from our user returns any rows, if it does we then display the form the users wants to access and then hide the login form until needed again.

now we have one final item to do and then this lesson is over, we need to make the login form visible after the user closes out the window they were at.

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		LoginForm1.Visible = True
	End Sub


the final item you need to do is make loginform1 be the first thing that displays and that can be done though the properties portion of the project.

the way the code works is when the user loads the program the login form displays, they enter their login code and then they have access to do whatever they wish to do. when their done they just simply close the window and then the login form will re-appear to either let another user login or to close out the program.

this is of course a simple example, later in this series I will go further into things.

feel free to comment and offer any additions to this.

if your stuck on anything with this, please let me know :)
0

#2 User is offline   Dylan Icon

  • Advanced Member
  • PipPipPip
Group:
Root Admin
Posts:
1,055
Joined:
12-January 07
Location:
Atlanta, GA, USA
Interests:
Computer Science, Operating Systems

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Mac OS X

Posted 09 November 2007 - 11:08 AM

Nice tutorial, vbnet_noob.

I've just submitted it to Pixel2Life.com
0

#3 User is offline   Mizukage09 Icon

  • Advanced Member
  • PipPipPip
Group:
Members
Posts:
116
Joined:
29-September 07
Interests:
Visual Basic, HTML, JavaScript, and video games.

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 09 November 2007 - 04:36 PM

I'm still confused.
0

#4 User is offline   Dylan Icon

  • Advanced Member
  • PipPipPip
Group:
Root Admin
Posts:
1,055
Joined:
12-January 07
Location:
Atlanta, GA, USA
Interests:
Computer Science, Operating Systems

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Mac OS X

Posted 09 November 2007 - 10:24 PM

View PostMizukage09, on Nov 9 2007, 04:36 PM, said:

I'm still confused.


What is causing you to be confused?
0

#5 User is offline   Mizukage09 Icon

  • Advanced Member
  • PipPipPip
Group:
Members
Posts:
116
Joined:
29-September 07
Interests:
Visual Basic, HTML, JavaScript, and video games.

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 10 November 2007 - 10:37 PM

View PostDylan, on Nov 9 2007, 10:24 PM, said:

What is causing you to be confused?

The code. lol. I guess I'm more of an HTML freak than a programming freak. Is there another way to skin that cat? Meaning is there a simpler way to write the code?
0

#6 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 11 November 2007 - 12:07 PM

Mizukage09, i know your pain very well.

much to say mysql was not really designed for vb but more for php, perl, java and C which have it built in and as such easier to code.

for vb and other languages that dont support it as well, you have to download & install a driver that acts as an add-on.

the one i use was here:

http://dev.mysql.com...or/net/5.0.html

this one was by far the best one as it uses the ADO.NET setup and not ODBC which is really slow and clunky.

now i'm not saying the method I posted is the only way, but based on what several concepts I tried and what I read based from the nice help file that came with the driver, this method worked best for me.

as to an easier method, this may be the easiest way im afraid, i tried something that was considered very basic in php and i had to add a few more lines of code to make the same concept work in vb.net

what portion seems to be confusing you the most?

btw thanks for the comment dylan and the submitting ;)
0

#7 User is offline   Mizukage09 Icon

  • Advanced Member
  • PipPipPip
Group:
Members
Posts:
116
Joined:
29-September 07
Interests:
Visual Basic, HTML, JavaScript, and video games.

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 11 November 2007 - 09:01 PM

View Postvbnet_noob, on Nov 11 2007, 12:07 PM, said:

what portion seems to be confusing you the most?


Everything. lol. I thought you could just enter code that does the following:

Take the username as a string
Take the password as a string
Have the database search through the Username column for the appropriate username
Match up the username with the password
Log In



But you had quite a package when I saw your code.
0

#8 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 11 November 2007 - 09:19 PM

much to say I did actually did that ;)

thats all it does is look through the DB and find a match. it just takes a few more lines of code to do it.
0

#9 User is offline   Mizukage09 Icon

  • Advanced Member
  • PipPipPip
Group:
Members
Posts:
116
Joined:
29-September 07
Interests:
Visual Basic, HTML, JavaScript, and video games.

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 12 November 2007 - 06:17 PM

Then I guess it was much more harder than I thought it would be.
0

#10 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 13 November 2007 - 08:09 PM

to be honest, i thought the same thing when i first was writing it esp. when in a totally different language i could have done it in 3 lines.

now i could do the same here but it'd require me to make the commands into a db class which lucky for you will be one of the tutorials I plan to write as it will be something you will want to keep forever(or until a new method comes around) ;)
0

#11 User is offline   m2098 Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
24-February 08

VB Knowledge: Beginner
VB Version:
VB 2005 (.NET 2.0)

Post icon  Posted 24 February 2008 - 05:48 AM

View Postvbnet_noob, on Nov 13 2007, 08:09 PM, said:

to be honest, i thought the same thing when i first was writing it esp. when in a totally different language i could have done it in 3 lines.

now i could do the same here but it'd require me to make the commands into a db class which lucky for you will be one of the tutorials I plan to write as it will be something you will want to keep forever(or until a new method comes around) ;)


Thanks for the tutorial I use this for my login

Private Sub ButtonLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
	
		Try

			If UserIDTextBox.Text = "" Then
				MessageBox.Show("Warning! No Data. Please enter your user ID")
				UserIDTextBox.Focus()
			Else

				If CType(Me.ManagersTableAdapter.MLogin(Me.UserIDTextBox.Text, Me.TextBoxPassword.Text), Integer) > 0 Then

					My.Forms.frmManager.Show()

				Else
					If CType(Me.CustomersTableAdapter.CLogin(Me.UserIDTextBox.Text, Me.TextBoxPassword.Text), Integer) > 0 Then

						My.Forms.frmCustomer.Show()
					Else
						If count > 0 Then
							MessageBox.Show("Warning! Wrong user ID or Password. You have " & count & " attempt(s) left")
							clearTextBoxes()
							UserIDTextBox.Focus()
							count = count - 1 'we set the counter to count here 

						Else
							MessageBox.Show("No more attempts left wrong user ID or password!")
							Me.Close()
						End If
					End If
				End If
			End If

		Catch ex As Exception
			MsgBox(ex.ToString)
		End Try

	End Sub


I want the new form that loads to only show the users data for the user that has just logged in, I have two table one customer and the other accounts, I know you can store the userID but I am not sure how I would reference it for the table fills if that makes sense

I currently have this code
Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim returnValue As DialogResult
        returnValue = My.Forms.Login.UserIDTextBox.Text()
        CustomerIDTextBox.Text = returnValue
        FillData()

    End Sub
    Private Sub FillData()
        Try
            Me.CustomersTableAdapter.Fill(Me.NorthernCrockDataSet.Customers, CustomerIDTextBox.Text)
        Catch ex As Exception

        End Try
        Try
            Me.AccountsTableAdapter.Fill(Me.NorthernCrockDataSet.Accounts, New System.Nullable(Of Integer)(CType(System.Convert.ChangeType(CustomerIDTextBox.Text, GetType(Integer)), Integer)))

        Catch ex As Exception

        End Try
    End Sub

But it gives me an error too meany arguments public override any ideas

thanks
M
0

#12 User is offline   jay-jay Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
16-May 08

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)

Posted 16 May 2008 - 08:56 PM

could someone help me with the code if i want to use ms access database...
0

#13 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 16 May 2008 - 09:44 PM

I have done some minor access work, the differences is minor.
0

#14 User is offline   drew4663 Icon

  • Newbie
  • Pip
Group:
Members
Posts:
2
Joined:
18-July 08

VB Knowledge: Beginner
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 18 July 2008 - 02:41 AM

I will be trying this method although I hvae tried something similar and the end result was a problem with the connection witht he MySql Server. I am using www.godaddy.com mysql server. I am not sure if it is something I am doing wrong or if it is something on their end. I am able to connect to it just fine using PHP scripting. I am currently going through a horrible process of user authentication and I am almost done but I think I have reached the end of my rope. Here's the scoop....

The user first opens the program after installation and is prompted only once to enter their information and create a username and password. When they commit it encrypts the info and saves it to the root installation folder as a text file. Only this file has my own custom file extension. During that process a form is opened and it displays a php file using the web that uploads the text file to a special folder. I am currently trying to get the vb.net folder to check for that file every time a different form is viewed.

This is all because I don't know how to make vb.net talk to mysql. So while I am testing this code out is there something different I need to be doing for a remote MySQL server? Thanks and sorry for the rambling.


Andrew
0

#15 User is offline   Dylan Icon

  • Advanced Member
  • PipPipPip
Group:
Root Admin
Posts:
1,055
Joined:
12-January 07
Location:
Atlanta, GA, USA
Interests:
Computer Science, Operating Systems

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Mac OS X

Posted 18 July 2008 - 03:40 PM

GoDaddy most likely doesn't allow their databases to be connected to from a remote source for security purposes. Your PHP website can connect to it because it's not remote.

I would contact GoDaddy and ask them if you can set your databases so you can remotely connect to them.
0

#16 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 18 July 2008 - 05:38 PM

i can already tell you that the answer is no.

i have been around the hosting biz and know godaddy is strict on things like that.

you'll need to install mysql on your own system in order to enjoy mysql and VB.

the php files will work on godaddy as their servers are set up for the database.

with mysql you can do remote connecting but the admin can select which machines have access.
0

#17 User is offline   drew4663 Icon

  • Newbie
  • Pip
Group:
Members
Posts:
2
Joined:
18-July 08

VB Knowledge: Beginner
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 19 July 2008 - 12:10 AM

hmmmm..... I do have PHP and MySQL installed on a server here locally. I do like the idea of just using the local database and the php files for godaddy.com. Thanks a heap for your wisdom and I will let you know of my success.


Andrew
0

#18 User is offline   ransom Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
02-September 08

VB Knowledge: Beginner
VB Version:
< VB 6

Posted 02 September 2008 - 10:09 AM

1) you didn't put the connector link as a required download at the beginning of the file/tutorial.. or am I to assume that it's included in express?

2) have you checked out http://dev.mysql.com...vbnet-2005.html it uses the http://toadsoft.com/toadmysql/ connector to give you an alternative & some "from the horses mouth" tutorials to look at..

3) I like how you write tutorials it's nice clean and very simple.. something you can put to use trying to figure out what part of the bridge is the support you can build your own bridge out of.. props for that & keep it up..

4) Why cant I post anonymously with a little ABC123 mash up/CATCHUP? I've noticed most vb forums are that way this one however at least pointed (somewhat) to the right thread instead of just saying "SEARCH" (though you could actual put a link in instead of just say it's in tutorials when their is no tutorial thread in ADO.NET for the quick fly by night surfer looking for help.)

5) Hope you got more praise out of this and all my criticism was constructive enough to not be considered a fly by flaming.


Thanks,

PS: why not attach the forum to the tutorial (DOWNLOAD CODE HERE) sorta thing..
0

#19 User is offline   vbnet_noob Icon

  • Moderator
  • PipPipPip
Group:
Mods
Posts:
441
Joined:
19-January 07

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)
OS: Windows Vista x64

Posted 02 September 2008 - 11:39 AM

hi,

first off we require registration for spam reason(allow guests to post is just not practical in todays world, plus it just clutter things up since you know most of them don't plan to return)

when it comes to MySQL, I like to use what they provide for libraries first, as if they make it its a shoe-in that it'll work. I have heard of this library, just never tried it(may do it sometime soon)

Quote

1) you didn't put the connector link as a required download at the beginning of the file/tutorial.. or am I to assume that it's included in express?


I'm sorry, most people who plan to use MySQL in VB.NET know that VS.NET as well as VB.NET is hinting that you use MSSQL or access, which while both are nice I like MySQL.

it would have been nice to post a link to the connector file(and for some reason i thought i did) but in any case when you install it, it should work for the express version, though having the professional edition, im not going to say you'll have as much support with it as you get with the pro version.

I do appreciate the positive comments you posted, I stride on clean & simple on anything I do programming-wise, I never got developers that made their codework so cryptic that only they know whats going on(some do it to ensure that they dont lose their job or if they do, the replacement will have their work cut out for them. i also think its bad practice)

Quote

PS: why not attach the forum to the tutorial (DOWNLOAD CODE HERE) sorta thing..


not sure what your trying to say there.
0

#20 User is offline   loatH Icon

  • Advanced Member
  • PipPipPip
Group:
Members
Posts:
61
Joined:
21-October 08
Location:
In Kernel ATM

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Windows XP

Posted 21 October 2008 - 04:24 PM

I have a problem, I set up a mysql database and I did it accurately but.

When I make my program and run it, it says, Cannot connect to the specified MYSQL host provided.

then the program stops responding and my compiler tells me
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
A first chance exception of type 'System.InvalidOperationException' occurred in MySql.Data.dll

in the immediate window and in my code window
'start query
		myAdapter.SelectCommand = myCommand
		Dim myData As MySqlDataReader
		[b]myData = myCommand.ExecuteReader()[/b]

where the bold text is it says InvalidOperationHandled Connection must be valid and open.

any help??
0

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users