pass string parameter from textbox in one window to another windiw in C#

Hi experts,

Here is my question:
This is in a WPF application using C#

In Window1.xaml

I have a textbox that looks like this:

<TextBox Height="23" Name="textBox1" Width="88" />

My code takes the string that was input in the textbox and puts it in a SQL server query and when a user presses a submit button it queries the database.

So in my code file: Window1.xaml.cs this is the code that handled the textbox string

        private string GenerateQuery()
            string val = null;
            string recordNumber = textBox1.Text;

            if (!(string.IsNullOrEmpty(recordNumber)))
                val = "SELECT RECORD_NUMBER, PAGES, RECORD_DATE FROM TABLE1 WHERE RECORD_NUMBER = '" + recordNumber + "'";

            return val;

That works fine and dandy.

So on another page called Window2.xaml

In part of the C# code

I have a query that looks like this

query += "FROM TABLE1 WHERE RECORD_NUMBER = '" + recordNumber + "'";

Is there a way that I can access the string variable recordNumber that was input into textbox1 in Window1 from my code in Window2?

I thought maybe i can declare the variable again in window2.xaml.cs and do it like this:
Saying "I am declaring a variable called recordNumber which is found in Window1"

string recordNumber = Window1.textBox1.Text;

But it's not liking that.     Has anyone done this before? Can anyone help?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
keep a static variable in either window or window2 and assign the value there instead of using the window controls directly.
One option is to create an external component for communication between these windows. In the simplest case it will just hold RecordNumber value and have reference to this component in both windows. Or it can expose an event RecordNumberChanged so that 2nd window can subscribe to it and get notifications.

The reference to this communication helper object can be set upon creation of windows. Something like that:

public class Mediator
    public string RecordNumber {get;set;}

public class App
    public void Func()
        Mediator mediator = new Mediator();
        Window1 w1 = new Window1(mediator);
        Window2 w2 = new Window2(mediator);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roshmon - that is the cheap way out and not a good practice to use.  

Mike has a good idea except for this promotes coupling between your classes.  I would look closely at your design to see if it is ideal (passing value from one window to the other doesn't make sense to me in the context you put it in).  

I think your best bet would be to keep a property in the calling application (in the example above this would be in the App class.  Since the App class is the "parent" of each of the windows you can reference back to the parent class without coupling the two windows together.  
Window1 and Window2 do not depend on each other. The only dependency is Mediator. Though to loosen coupling between classes more we can extract an interface of Mediator.

public interface IMediator
    string RecordNumber {get;set;}

This is still just a quick fix. Without knowing the context it's difficult to suggest an optimal solution. So only the direction to move in...
maqskywalkerAuthor Commented:

Your suggestion helped me get unstuck.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.