Tuesday 16 December 2014

Session is not applicable in Windows Application

Session is not applicable in Windows Application :
Session or ViewState is not applicable in Windows Application. We are using Session in Web
Application becuase http is a stateless protocol and we need to maintain state of the application
using any of the state management technology available like session, query string. These state
management technologies are not available for Windows Application.

In Windows Form, if you want to share a object in different forms, you may use global objects.

Use a global class file to get and set the values of an object

Create a class Class1.cs and add the following code:
class Class1
    {
        static String str = "";
        public static String GetData()
        {
            return str;
        }

        public static void SetData(String str1)
        {
            str = str1;
        }
    }

Now Create two Form.
In First Form take one TextBox and One Button
Add the following code in Button Click Event:
 private void button1_Click(object sender, EventArgs e)
        {
            Class1.SetData(textBox1.Text.Trim());

            Form2 f = new Form2();
            f.Show();
        }

In Second Form take one Label
Write the following code in Form Load event of Form2:
 private void Form2_Load(object sender, EventArgs e)
        {
            label1.Text = Class1.GetData();
        }
*** Happy Coding***