Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 812
  • Last Modified:

bind string array to listbox

so i query an excel file and am trying to bind the worksheets to a list box. i get them in the list box but also get an error back:

Object reference not set to an instance of an object.

string[] strTables = GetTableExcel(SpreadSheet); //pass path and spreadsheet file name
//like: c:\test\test.xls
                            lstbxWorkSheets.DataSource = strTables;
                            lstbxWorkSheets.DataBind();


public static string[] GetTableExcel(string strFileName)
        {
            string[] strTables = new string[100];

            Catalog oCatlog = new Catalog();
            ADOX.Table oTable = new ADOX.Table();
            ADODB.Connection oConn = new ADODB.Connection();
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties='Excel 12.0;HDR=YES';";

            try
            {
                oConn.Open(connectionString);
                oCatlog.ActiveConnection = oConn;
                if (oCatlog.Tables.Count > 0)
                {
                    int item = 0;
                    foreach (ADOX.Table tab in oCatlog.Tables)
                    {
                        if (tab.Type == "TABLE")
                        {
                            strTables[item] = tab.Name;
                            item++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message.ToString();
            }

            return strTables;
        }

Open in new window


i don't quite understand why i get an error back
0
fwstealer
Asked:
fwstealer
1 Solution
 
Rajar AhmedConsultantCommented:
how about ArrayList ?
public static ArrayList GetTableExcel(string strFileName)
        {
           // string[] strTables = new string[100];
           ArrayList strTables = new ArrayList();
            Catalog oCatlog = new Catalog();
            ADOX.Table oTable = new ADOX.Table();
            ADODB.Connection oConn = new ADODB.Connection();
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties='Excel 12.0;HDR=YES';";

            try
            {
                oConn.Open(connectionString);
                oCatlog.ActiveConnection = oConn;
                if (oCatlog.Tables.Count > 0)
                {
                    int item = 0;
                    foreach (ADOX.Table tab in oCatlog.Tables)
                    {
                        if (tab.Type == "TABLE")
                        {
                            //strTables[item] = tab.Name;
                            strTables.Add(tab.Name);  
                            item++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message.ToString();
            }

            return strTables;
        }

Open in new window

0
 
informaniacCommented:
Replace the string array with

List<string> str = new List<string>();

and bind the listbox to the list object.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now