zana
zana

Reputation: 269

Getting selected dialog item to another button method

So I'm writing a windows form application where the user will select a xml file (using file dialog) from his computer and when he clicks the save button, it should save in the database. But I'm a little lost on how to get the selected item to the save button method. The following is my btnChooseFile_click

private void btnChooseFile_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Multiselect = false;
        dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            tbxXmlFile.Text = dlg.FileName;
            XmlDocument invDoc = new XmlDocument();
            invDoc.Load(dlg.FileName);
            ....
            ....
        }
    }

And below is my btnStore_click

  private void btnStore_Click(object sender, EventArgs e)
    {
        try
        {
            string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
            using (SqlConnection sqlConn = new SqlConnection(cs))
            {
                DataSet ds = new DataSet();
                ds.ReadXml("This is where I want to get the selected file");
                ....
                ....
            }
        }
    }

So how do I go about this?

Upvotes: 1

Views: 46

Answers (1)

Ralston
Ralston

Reputation: 1869

You can use a private member variable

private String filename = null;

private void btnChooseFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Multiselect = false;
    dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        tbxXmlFile.Text = dlg.FileName;
        XmlDocument invDoc = new XmlDocument();
        invDoc.Load(dlg.FileName);
        ....
        ....
        this.filename = dlg.FileName;
    }
}

private void btnStore_Click(object sender, EventArgs e)
{
    try
    {
        string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
        using (SqlConnection sqlConn = new SqlConnection(cs))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(this.filename);
            ....
            ....
        }
    }
}

Upvotes: 1

Related Questions