Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot access DataTable in DataSet by name

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have the following code that will add a new DataTable (excel sheet) to a DataSet. I am trying to do it by name so I can access the table like: ds.Tables["EmployeeNames"]


The problem is that if I do:

ds.Tables[0] - it works fine. The debugger shows the table as "EmployeeNames" and the TableName shows as "EmployeeNames".

But if I do: ds.Tables["EmployeeNames"]

I get a null.

Here is the code I am using:

Code:
public System.Data.DataSet GetWorksheet()
{
    DataSet ds = new DataSet();

    foreach (string ws in workSheetNames)
    {
        ds.Tables.Add(GetWorksheet(ws));
    }
    return ds;
}

public System.Data.DataTable GetWorksheet(string worksheetName)
{
    OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
    OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(
        "select * from [" + worksheetName + "]", con);

    con.Open();
    System.Data.DataTable excelDataTable = new DataTable(worksheetName.Replace("$",""));
    cmd.Fill(excelDataTable);
    con.Close();

    return excelDataTable;
}

How can I call this so I can access the database by name?

Thanks,

Tom

 
I just found that if I put single quotes around the name inside the double quotes, it seems to work ????

so this works:

ds.Tables["'EmployeeNames'"]

but this does not:

ds.Tables["EmployeeNames"]

Is it the way I created the datatables and dataset?

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top