Open Up An MDI Form

  • 9 years ago
    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:


    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

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The trouble with programmers is that you can never tell what a programmer is doing until it's too late.” - Seymour Cray