Solved

Opening up new form

Posted on 2008-10-14
6
290 Views
Last Modified: 2013-12-17
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
Comment
Question by:MichelleLacy
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22710553
-->"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
 

Author Comment

by:MichelleLacy
ID: 22711241
Is there a way to do this in the scenario I put forward, instead of re-writing my code as you suggested?
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 400 total points
ID: 22711615
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Assisted Solution

by:Reddgum
Reddgum earned 100 total points
ID: 22711677
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
 

Author Comment

by:MichelleLacy
ID: 22721425
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
 

Author Closing Comment

by:MichelleLacy
ID: 31505845
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now