• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Opening up new form

I user goes to the start page and select a qeury form from the menu stirp.  This form opens and it requests data from the database upon clicking a button.  After the button is click a new form is opened and the data is displayed in a table.  I want the user to be able to select cells from this table, click a button which opens up another form in the Main Form, and puts the data into a textbox.
Now I was given a solution that worked wonderfully for a similar scenario;  I.E. the user selected a form from the menu strip, the form opened and then the user requested information from the database via a form.  the data was returned back to that same form (it did not send the data to a new form).  I was able to use the follow method to open my target form in the main window:
      Form2 f2 = new Form2();
        f2.TopLevel = false;
        f2.Location = new Point(this.Left + this.Width, 0);
        this.Parent.Controls.Add(f2);
        f2.Show();

However, this not work for my current scenario, where the data is returned to another form.  I am not sure why, please advise.  I have pasted the error message below.
System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="TxProfile2_Query_Prototype"
  StackTrace:
       at TxProfile2_Query_Prototype.TissueSpecificGeneList.retrieveProbeSelection() in C:\Documents and Settings\MMLACY\My Documents\Visual Studio Projects\TxProfile2_Query_Prototype\TissueSpecificGeneList.cs:line 127
       at TxProfile2_Query_Prototype.TissueSpecificGeneList.btnGeneProfile_Click(Object sender, EventArgs e) in C:\Documents and Settings\MMLACY\My Documents\Visual Studio Projects\TxProfile2_Query_Prototype\TissueSpecificGeneList.cs:line 144
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
       at System.Windows.Forms.Form.ShowDialog()
       at TxProfile2_Query_Prototype.TissueProfileSearch.btnSubmit_Click(Object sender, EventArgs e) in C:\Documents and Settings\MMLACY\My Documents\Visual Studio Projects\TxProfile2_Query_Prototype\TissueProfileSearch.cs:line 611
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WmCommand(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TxProfile2_Query_Prototype.TxProfileHomePage.Main() in C:\Documents and Settings\MMLACY\My Documents\Visual Studio Projects\TxProfile2_Query_Prototype\TxProfileHomePage.cs:line 320
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Open in new window

0
MichelleLacy
Asked:
MichelleLacy
  • 3
  • 2
2 Solutions
 
Anurag ThakurTechnical ManagerCommented:
-->"the form opened and then the user requested information from the database via a form.  the data was returned back to that same form"
i will suggest that then new form opened and then the database hit made to fetch the data instead of doing it on the first form
0
 
MichelleLacyAuthor Commented:
Is there a way to do this in the scenario I put forward, instead of re-writing my code as you suggested?
0
 
Anurag ThakurTechnical ManagerCommented:
if i am getting data on form 1 on the button click and i am passing it to from 2 then i will be using the constructor of the Form2 to pass that data
        Form2 f2 = new Form2(dataSetReturnedFromDBQueryOnForm1);
        f2.TopLevel = false;
        f2.Location = new Point(this.Left + this.Width, 0);
        this.Parent.Controls.Add(f2);
        f2.Show();

anyways... after looking at your exception i was not able to figure out the actual reason for that, please tell us the exact circumstances under which you get the exception and the line of code producing the same
The exception clearly states "Object reference not set to an instance of an object" which object is not initialized before using???
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
ReddgumCommented:
Hi Michelle,

Without really seeing snips of the code in question here, I'm going to take a stab and guess that you're running into object de-referencing as a consequence of switching active forms. When you work with multiple forms, understand that each operates within a different thread and the objects contained within those forms (like text boxes etc) and will need to be invoked if you wish to use them.

I use a delegate pattern to move data between my windows apps that use multiple forms. Attempting to populate one form from another that accesses a database sounds like a perfect scenario for this - so in your form where the DB query takes place raises an event that is captured by the form where your grid shows up, and when the user is done clicking on the data columns they need, clicking a button (OK) fires another event caught by the main form. In both cases, you can use the same container class.

You really should take the time to learn how to use delegates and events, because they will make your life so much easier in winForms development.


Delegate example:
http://msdn.microsoft.com/en-us/library/900fyy8e.aspx

Events:
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx

HTH,

-Red

0
 
MichelleLacyAuthor Commented:
I read a little about events and delegates and looked how the default delegates worked in my application.  It looks like the way to go, but how would you use a delegate in to solve my problem.  As I understand it,
The event is a button click, the method called is:

private void retrieveSelection()
{
ProfileQuery frm = new ProfileQuery();
frm.TopLevel = false;
tissfrm.Location = new Point(this.Left + this.Width, 0);
this.Parent.Controls.Add(frm);
frm.Show();
frm.cmboItem.SelectedValue = this.cmboItem.SelectedValue;
frm.rdoProbeSet.Checked = true;
foreach (DataGridViewCell cell in this.dvg.SelectedCells)
{
userSelectedCells.Add(cell.Value.ToString());
}
foreach (string str in userSelectedCells)
{
frm.txtID.AppendText(str.ToString() + "\n");
}
}
 
How do I use a delegate in place of this method,  where it goes through and gets the selected cells from datagridview dvg, adds them to the List<string> userSelectedCells; then appends them to a multi-line text box in the ProfileQuery frm, and finally it is selects the ProfileQuery frm that is selected in the current form?
0
 
MichelleLacyAuthor Commented:
Well I tried several different scenarios to get this to work.  I did implement an event/handler/delegate type process, it did not change the outcome.  The good news is, I created an independent class for the method that uses a form to retreive data from the database and send it to another form.  I use this method in several of my other forms, so instead of repeating it I created a separate class and the forms just use that class.  It did indeed help me learn how to use delegates and also cleaned up my app.  But as far as the solution to this problem, sending the data back to the orginating form was the only solution that worked.  As I stepped through in debugging mode, I discovered that since the second form is not a child of the mdi parent, even the originating form, I could not get it to work.  I even tried to force it by hardcoding it in, but to no avail.  So with a little re-arranging and hiding and unhiding controls I was able to get everything into one form(which is an mdi child) and it worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now