[RESOLVED] Object not set error
Hello,
This should be an easy thing to do, but I can't figure out what I am doing wrong.
I have a codejock report control on the main form that list users in a database. After a new user is added thru a different form, I want the list to update. This should be easy, but I get an "object reference not set to an object" error. The code that does the update is in a class. When the user selects the option to view the users, then report control is passed byref to the sub as a parameter. On the second form I dim frm as frmMain(my main form that holds the report control) and do frm.PEPReportControl when I call the sub. The code is below of two of the things I have tried.
My form class is public, my class is public. I have done searching on the web, but haven't found any help. Also, I don't get any lines under the code. I have tried creating a reference to the form, and the control type, and pass that in the parameter, but that didn't work either.
Any help would very good. Thank you.
VB Code:
Try
Dim frm As frmMain
Dim RC As AxXtremeReportControl.AxReportControl = frm.PEPReportControl
MyServerControl.AddUser(Me.UserName.Text.ToString, Me.Password.Text.ToString, Me.FName.Text.ToString, Me.LName.Text.ToString)
MyFormControl.PopulateReportControlUserInfo(RC)
frm = Nothing
Me.Hide()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
VB Code:
Try
Dim frm As frmMain
MyServerControl.AddUser(Me.UserName.Text.ToString, Me.Password.Text.ToString, Me.FName.Text.ToString, Me.LName.Text.ToString)
MyFormControl.PopulateReportControlUserInfo(frm.PEPReportControl)
frm = Nothing
Me.Hide()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Re: [RESOLVED] Object not set error
If you're displaying a modal dialogue then it is generally not appropriate for the dialogue to access the caller. The more correct way to do it is for the dialogue to expose whatever data is required via properties or methods and for the caller to retrieve it once ShowDialog returns. Here's an example of what that might look like.
In Form1:
VB Code:
Dim f2 As New Form2
If f2.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Get the data from the dialogue and display it.
Me.TextBox1.Text = f2.Value1
Me.TextBox2.Text = f2.Value2
End If
f2.Dispose()
It's up to you to declare the appropriate properties in Form2 to expose the required data, e.g.
VB Code:
Public ReadOnly Property Value1() As String
Get
Return Me.TextBox1.Text
End Get
End Property
Public ReadOnly Property Value2() As String
Get
Return Me.TextBox2.Text
End Get
End Property
Now the dialogue is completely independent of the caller. The dialogue just appears, does its thing and disappears and this can be done from anywhere at any time. The Form2 doesn't care who calls it when or where. It just exposes the appropriate data and whoever wants it can get it. That's much better than the dialogue depending on being called by a specific type of form.
Re: [RESOLVED] Object not set error
So then I should move the code that calls the sub to add the user to the database from the modal form to the main form, correct?
Also, this form is for adding and editing users in the database. With the editing, should I make the properties both get and set? Then when I create and instance of the form I can load the values into the properties, then in the set put me.username.text = value?
Re: [RESOLVED] Object not set error
That all sounds reasonable. There's also another option. Your caller could pass a DataRow to the dialogue. The dialogue could then edit that DataRow directly and neither form needs to access the other at all. When the user presses the OK button the dialogue simply updates the DataRow object it was passed and that's it. Those changes will be reflected in the caller because it references the same DataRow object.
Form1:
VB Code:
Private Sub AddNewRow()
Dim dr As DataRow = myDataTable.NewRow()
Dim f2 As New Form2(dr)
If f2.ShowDialog() = Windows.Forms.DialogResult.OK Then
myDataTable.Rows.Add(dr)
End If
End Sub
Private Sub EditExistingRow(ByVal index As Integer)
Dim f2 As New Form2(myDataTable.Rows(index))
f2.ShowDialog()
End Sub
Form2:
VB Code:
Private data As DataRow
Public Sub New(ByVal data As DataRow)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.data = data
Me.TextBox1.Text = CStr(data("Column1"))
Me.TextBox2.Text = CStr(data("Column2"))
End Sub
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
Me.data("Column1") = Me.TextBox1.Text
Me.data("Column2") = Me.TextBox2.Text
End Sub
Re: [RESOLVED] Object not set error
That's true, I already did the first option, but I am going to be thinking about this project before I go to bed, so I will definently give it some more thought.
Thank you for your help.