Click here to Skip to main content
16,013,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Object reference not set to an instance of an object



C#
Form2 fr2 = new Form2();
                    
                     string srv = fr2.sqlServerComboBox.SelectedItem.ToString();
                     string pass = fr2.passwordTextBox.Text.ToString();
                     string database = fr2.databaseComboBox.SelectedItem.ToString();
                   

                   string str = @"Data Source=srv;" + "Initial Catalog=database;" + "Persist Security Info=True;" + "User ID=sa;" + "Password=pass";
Posted
Updated 9-Feb-16 1:15am
v2

You're not showing the form so unless you have code in Form2 that automatically selects something on the combobox nothing is going to be selected. Also if you haven't made those controls public then you're not going to be able to access them.
Have a look at OriginalGriff's article Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
You are requesting the property of something that is null (ie a.b where "a" is null). We can't run your code of know your inputs so it's impossible for us to help really. It's likely that simply creating an instance of Form2 is not selecting items in your dropdowns so maybe SelectedItem is null? You'll need to use the debugger to find out what is null.

c# - What is a NullReferenceException and how do I fix it? - Stack Overflow[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
v3

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



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