Opening an MDI Main Form

mejames

New member
Joined
Jan 12, 2008
Messages
1
Programming Experience
Beginner
I have an application that I am building. I have a normal windows form as a login. If the users are accepted, it opens up a MDI parent form as the frmMain. However, when I try to open the form, I am getting an error : Object Reference not set to an instance of an object.

Below is the code for the frmLogin that calls the frmMain. If i start my project to start wtih the frmMain instead of the frmLogin, it opens right up with no problem. When I switch it back to start with the frmLogin, I receive the error. Here is my code:


VB.NET:
Public Class frmLogin
Inherits System.Windows.Forms.Form
'Path for Database
Public strPath As String = System.AppDomain.CurrentDomain.BaseDirectory & "\snma.mdb"
Dim fMain As frmMain

Dim dapEmployee As OleDb.OleDbDataAdapter
Dim myDataSet As New DataSet
Dim ID As Integer
Dim fName As String
Dim lName As String
Dim instructor As Integer
Friend WithEvents SkinEngine1 As Sunisoft.IrisSkin.SkinEngine
Dim permission As String


Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
CheckUser()
If ID > 0 Then
GetEmployeeInformation(ID)
fMain.Member_ID = ID
fMain.Member_FirstName = fName
fMain.Member_LastName = lName
fMain.Member_Permission = permission
Try
fMain.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try

Me.Hide()
Else
MsgBox("Invalid Username and Password")
End If
End Sub

Private Function CheckUser() As Integer
Dim cnn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath)
ID = 0
If txtUsername.Text <> "" And txtPassword.Text <> "" Then
Dim tmpCmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT ID FROM Employees WHERE Username='" _
& txtUsername.Text & "' AND Password='" & txtPassword.Text & "'", cnn)
Try
cnn.Open()
ID = tmpCmd.ExecuteScalar
Catch ex As Exception
Finally
cnn.Close()
End Try
End If
End Function

Private Sub GetEmployeeInformation(ByVal intID As Integer)
Dim cnn As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath)
Dim sql As String = "SELECT * FROM Employees WHERE id=" & intID
dapEmployee = New OleDb.OleDbDataAdapter(sql, cnn)
dapEmployee.Fill(myDataSet, "employees")
myDataSet.Tables("employees").PrimaryKey = _
New DataColumn() _
{(myDataSet.Tables("employees").Columns("id"))}
Dim drw1 As DataRow = myDataSet.Tables("employees").Rows.Find(CInt(intID))
Try
If Not (drw1 Is Nothing) Then
If Not drw1.IsNull(0) Then ID = drw1.Item(0)
If Not drw1.IsNull(1) Then fName = drw1.Item(1)
If Not drw1.IsNull(2) Then lName = drw1.Item(2)
If Not drw1.IsNull(5) Then permission = drw1.Item(5)
End If
Catch ex As Exception
End Try
End Sub

Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fMain = New frmMain
End Sub
End Class
 
Last edited by a moderator:
I would set main form as startup form and use the Application Startup event to show the login dialog, if failing login the application startup would be cancelled. Check threads http://www.vbdotnetforums.com/showthread.php?t=22002 and http://www.vbdotnetforums.com/showthread.php?t=19637

You can for example transfer the ID to main form with a user application setting. Another option is a property of main form, set this by accessing default form instance (frmMain.id = 3).

Also change the Select queries to use Parameters.
 
Back
Top