Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i have to pass combobox selected text in frmpopupform and create variable but bot set refernces

What I have tried:

//frmpopupform code

frmInvoice frmobject = new frmInvoice();
string str = frmobject.comboBox1.SelectedItem.ToString();
Posted
Updated 12-May-16 4:02am
v3
Comments
Karthik_Mahalingam 12-May-16 0:44am    
Provide more info the question. Use Improve question

There is a problem in your code, you just create a new object of form and accessing combobox from it, How it is possible ?
When you create a new object of a form all the control values are NULL/Nothing you can not access value from it.
What you exactly want to do ? if you want to transfer values from oneform to another check below link
Transfer data from one form to another form in Windows Application using C#[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-May-16 1:35am    
Who, who told you that control values are nulls after for creation?

Not only this assumption is not based on anything, it is highly unlikely, especially if you take into account the inquirer's level. Most likely, this person, being a beginner, simply added all controls using the designer. And the designer set up all controls and called that function in the constructor.

Who told you that the question is related to "transfer data from one form to another form"?

—SA
koolprasad2003 12-May-16 1:53am    
Thanks for the elaboration SA but Please have a look at code once, OP has just create NEW object of form and trying to access its control values, I am bit sure there is nothing he has assign in constructer (in InitilizeComponenet method), if he has values to assign in constructor to controls why he can't directly get it from there ? why to access controls for that ?
OP has two forms 'frmpopupform' and 'frmInvoice', he has to pass combobox selected text in frmpopupform.
Here he got Object reference error due to 'frmobject.comboBox1.SelectedItem' is Null for sure I guess.
Sergey Alexandrovich Kryukov 12-May-16 2:32am    
You just say right now that you have a fundamental misconception about object creation. You don't understand that "new" calls the constructor, always, and the constructor does what it does. Moreover, for classes, members are typically constructed before the constructor of its derived class.

There is no "frmpopupform" in the code, so we cannot know what it can be. It can be anything.

—SA
koolprasad2003 12-May-16 3:13am    
Just a throught:
SA, look at the first line of "what I tried" section, OP wrote '//frmpopupform code' means the code he has written in 'frmpopupform' and want to to pass combobox selected text in frmpopupform.
Sergey Alexandrovich Kryukov 12-May-16 8:34am    
And?
—SA
Most likely, c is not null, because the constructor should create this instance, but nothing is selected. Then frmobject.comboBox1.SelectedItems returns null, which is fine. But then you try to dereference this null object by calling its instance method ToString(); and this is what throws this exception. Apparently, you should not do such things before you set some selection.

But you cannot ask such questions every time similar things happen. It's very important to cope with such situations by yourself. Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

By the way, never ever use auto-generated names such as comboBox1. They violate (good) Microsoft naming conventions (by obvious reasons). You are supposed to rename everything to give members semantically sensible names; only then you can develop maintainable code which is not pain to look at. After all, you are given the refactoring engine in Visual Studio.

Good luck,
—SA
 
Share this answer
 
v2
The answer is probably simple. I do not know completely how you build your solution, but SelectedItem can be null if nothing selected in ComboBox.

Try something like this, probably it will help:
C#
frmInvoice frmobject = new frmInvoice();

string str;

if (frmobject.comboBox1.SelectedItem != null)
    str = frmobject.comboBox1.SelectedItem.ToString();
else
    str = "None selected";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900