Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey team. Im very new to C# and kindly don't regret if there are mistakes.
To read 2nd and 3rd columns in a excel file in data table and display it in a list view with check boxes.
Thanks in advance for the help!!

What I have tried:

Created two functions separately. One for initialising OLEDB and other to read the file in data table. And finally using the button action the data table shall be called and the excel file shall be displayed in the list view with check boxes.

NOTE:
The excel file consists of 5 sheets and out of those 5 i want to read the 2nd and 3rd column of 2nd sheet.
clicking next button will navigate to next tab where list view box is present.

C#
  void InitializeOledbConnection(string filename, string extrn)
        {
            connString = "";

            if (extrn == ".xlsx")

                connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + glb + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"; //for above excel 2007
            else

                connString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + glb + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"; //for below excel 2007

            Oledb = new OleDbConnection(connString);

        }

private DataTable ReadFile()

        {
                schemaTable = new DataTable();
                OleDbCommand Oledbcmd = new OleDbCommand();
                Oledbcmd.Connection = Oledb;
                Oledb.Open();                
              //using (OleDbConnection Oledb = new OleDbConnection(connString))                
                Oledbcmd.CommandText = "Select * from [CONTROL_TABLE$]";
                OleDbDataReader dr = Oledbcmd.ExecuteReader();
                ContentTable = new DataTable();
                ContentTable = null;

            if (dr.HasRows)

                {
                    ContentTable = new DataTable();                    
                    ContentTable.Columns.Add("SIGNAL", typeof(string));
                    ContentTable.Columns.Add("ROUTE_BUTTON", typeof(string));               

                while (dr.Read())

                {
                    if (dr[0].ToString().Trim() != string.Empty && dr[1].ToString().Trim() != string.Empty && dr[2].ToString().Trim() != string.Empty && dr[0].ToString().Trim() != " " && dr[1].ToString().Trim() != " " && dr[2].ToString().Trim() != " ")
                    {
                        ContentTable.Rows.Add(dr[0].ToString().Trim(), dr[1].ToString().Trim(), dr[2].ToString().Trim());
                    }
                }
            }

                dr.Close();
                Oledb.Close();
                Oledb.Dispose();
                return ContentTable;   
                     
           }

private void t1NextTabRedirectButton_Click(object sender, EventArgs e)
        {
            tabControl.SelectedTab = routeNameTabPage;

            {
                string filePath = string.Empty;
                string fileExt = string.Empty;

                InitializeOledbConnection("C:\\gui_Files\\ICT_LAYOUT_AJJ.xlsx", ".xlsx");
                DataTable tempTable = ReadFile();

                {
                    for (int i = 0; i < schemaTable.Rows.Count; i++)
                    {

                        glb = schemaTable.Rows[i]["SIGNAL"].ToString();
                        glb = schemaTable.Rows[i]["ROUTE_BUTTON"].ToString();

                    }
                    routeNamesListView.Columns.Add("SIGNAL");
                    routeNamesListView.Columns.Add("ROUTE_BUTTON");
                    routeNamesListView.View = View.List;
                    routeNamesListView.CheckBoxes = true;                    
                }
            }
        }
Posted
Updated 28-Dec-17 2:06am
v4
Comments
Richard MacCutchan 27-Dec-17 5:32am    
And what is your question?
Arvi.S 27-Dec-17 5:41am    
To read 2 columns in a excel sheet and should display the values in the list view control with checkboxes
Richard MacCutchan 27-Dec-17 5:52am    
Yes, you already said that, but you have not explained what problem you have with the above code.
Arvi.S 27-Dec-17 5:56am    
It is showing me "An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll" pointing the
"ContentTable.Rows.Add(dr[0].ToString().Trim(), dr[1].ToString().Trim(), dr[2].ToString().Trim());"

Since im new to C#, i don't have any idea why to use that "dr[]".
Richard MacCutchan 27-Dec-17 6:30am    
Then there really is not much point in me trying to explain it to you. How did you manage to write this code if you don't understand it? As a basic answer, you should not use compound statements like that before checking that each of the elements you are trying to reference actually exist. All we can say is that one of those references is probably a null value - but that is still a guess. The only way to be sure is to use the debugger.

1 solution

As mentioned in the comments, the first step is to understand the code. You have something that is null but we cannot tell you why because we cannot run your code in your environment with your data.
 
Share this answer
 

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