Using form inheritance for database connection.


Jun 27, 2012
Programming Experience
Hello everyone.
I am fairly new to VB.NET. I am building a simple mobile application (WM 6.0) in which I have a login form (where the user inputs his username and password) and subsequent forms that capture certain fields that are to be inserted into the same database that contains the username and password table. What I did was establish a connection in the login form using:

Dim conn As SqlCeConnection = Nothing
conn = New SqlCeConnection("Data Source = Storage Card\MyDB.sdf; Password ='12345678'")
Catch er As Exception
MessageBox.Show("Database file missing. Contact system Administrator")
End Try

and then do this when the login button is clicked:

Dim cmd As SqlCeCommand = conn.CreateCommand()
Dim dr As SqlCeDataReader

cmd.CommandText = ("SELECT Password FROM Users WHERE EmpID = '" & usernameTextbox.Text & "'")
dr = cmd.ExecuteReader

.....and so on.

The problem is, all subsequent forms require the connection to the DB too. For example, a change password form that updates the password field for that particular user. How can I create the connection to the DB once and then inherit that functionality to all the forms? Thanks in advance.
Top Bottom