Click here to Skip to main content
14,740,560 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have two combobox (combobox1 in form 1 and combobox2 in form2). I need to import an excel file and the combobox1 gets the sheets name. Then in the form2 (I don't want to import the same file another time) i have combobox2 that have also the same sheets name from the combobox1.

I tried this but i got the error of NULLFUNCTION Expression(Additional Information: The object reference is not set to an instance of an object).

What I have tried:

using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx |fichiers Textes (*.txt)|*.txt|fichiers CSV (*.csv)|*.csv|tous les fichiers (*.*)|*.*" })
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    TextBox_Emplacement.Text = openFileDialog.FileName;
                    using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                            });
                            dataTableCollection = result.Tables; 
                            dataTableCollection1 = result.Tables;
                            sheet.Items.Clear();
                            //cp.radDropDownList1.Items.Clear();
                            foreach (DataTable table in dataTableCollection)
                                sheet.Items.Add(table.TableName);//add sheet to combobox

                           Form_Copie cp1 = (Form_Copie)Application.OpenForms["Form_Copie"];
                           foreach (DataTable table in dataTableCollection)
                              cp1.radDropDownList1.Items.Add(table.TableName);//add sheet to combobox
Posted
Updated 13-Jan-21 11:17am
Comments
Richard Deeming 13-Jan-21 5:08am
   
REPOST
This is the same question you posted yesterday:
How to save imported excel path in background to avoid importing multiple times with C#[^]

If you want to update your question, click the green "Improve question" link and edit your question. Do not post the update as a new question.

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
   
Comments
houssem eddine ayari 13-Jan-21 5:29am
   
@OriginalGriff there is no relationship between my two forms
OriginalGriff 13-Jan-21 5:38am
   
If they are both in the same app, there is a relationship between them!
Either one creates an instance of the other, or they are both created by a third form, or forms.
If they are in different applications, then that a whole new ball game - and a complicated one!
<pre>using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx |fichiers Textes (*.txt)|*.txt|fichiers CSV (*.csv)|*.csv|tous les fichiers (*.*)|*.*" })
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    TextBox_Emplacement.Text = openFileDialog.FileName;
                    using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
                    {
                        using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                            {
                                ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                            });
                            dataTableCollection = result.Tables; 
                            dataTableCollection1 = result.Tables;
                            sheet.Items.Clear();
                            //cp.radDropDownList1.Items.Clear();
                            foreach (DataTable table in dataTableCollection)
                                sheet.Items.Add(table.TableName);//add sheet to combobox

                           Form_Copie cp1 = (Form_Copie)Application.OpenForms["Form_Copie"];
                           foreach (DataTable table in dataTableCollection)
                              cp1.radDropDownList1.Items.Add(table.TableName);//add sheet to combobox
   
Comments
Richard Deeming 14-Jan-21 5:49am
   
An unexplained code-dump is not a "solution" to this question.

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