Essa Khan Essa Khan - 10 days ago 6
C# Question

How can I access data created in one event from another event?

I want to access obtain data created in the

Load
event in the
TextBox.TextChanged
event.

How do I do this?

private void Login_Load(object sender, EventArgs e)
{
//Instantiating DB Obj
DataBaseConnection obtainData = new DataBaseConnection();

//Filling dataset
obtainData.passSqlCmdandFillDs = "select * from [tblLogin]";
}

private void txtDisplay_TextChanged(object sender, EventArgs e)
{
//Want to user obtainData here
}

Answer

Declare obtainData outside of the Login_Load event so it's available in other methods.

DataBaseConnection obtainData;

private void Login_Load(object sender, EventArgs e)
{
    //Instantiating DB Obj
    obtainData = new DataBaseConnection();

    //Filling dataset
    obtainData.passSqlCmdandFillDs = "select * from [tblLogin]";
}

private void txtDisplay_TextChanged(object sender, EventArgs e)
{
    if (obtainData != null)
    {
        // Use obtainData here
    }
}