Cannot apply indexing with [] to an expression of type 'object'

  • Thread starter Thread starter Bassem
  • Start date Start date
B

Bassem

Hi to all...
Thanks for the help...

I want to get the value of the column of dataset into array, but I get this bug.

"Cannot apply indexing with [] to an expression of type 'object'" when biuld.

The code I used to populate the array is:

ColumnNo = from 0 TO count of columns.

AllColumns[ColumnNo] = dataSet11.EmployeeSalary.Columns[ColumnNo][1];

Thanks for the help.
 
Do you really want to be looking into the DataColumn objects, or are you
after the values on a row? You don't use the Columns member to get at
column *values* for a row of data. I'm not sure what you intend with the
[1] at the end either.

Brad Williams
 
Thanks for your replay,

Simply I want to get the value of the columns in the array I create to
be able to add them in text file.
Thus if you put this line of command in the command windows will give
you the value of the columns.
dataSet11.EmployeeSalary.Columns[ColumnNo][1]

But if you will get to use it in the editor window to write the code it
will not compile.

This is the problem , the line doesn't work.
Thanks for you.
 
Bassem bassem said:
Thanks for your replay,

Simply I want to get the value of the columns in the array I create to
be able to add them in text file.
Thus if you put this line of command in the command windows will give
you the value of the columns.
dataSet11.EmployeeSalary.Columns[ColumnNo][1]

But if you will get to use it in the editor window to write the code it
will not compile.

This is the problem , the line doesn't work.

You need to give us more information - such as the type of the
EmployeeSalary property and the Columns property, for a start.
 
Hi Jon,

This is the lines that give to you the information that you want and the
problem I have.

int FoundRec = 0 ;
oleDbDataAdapter1.SelectCommand.CommandText = "SELECT * FROM
EmployeeSalary WHERE EmployeeName = '"+ txtBox.Text.Trim() +"' ";
dataSet11.Clear();
oleDbDataAdapter1.Fill(dataSet11, "EmployeeSalary");
FoundRec = dataSet11.EmployeeSalary.Rows.Count;
if (FoundRec > 0)
{
int ColumnsCount = dataSet11.EmployeeSalary.Columns.Count;
string[] AllColumns = new string[ColumnsCount];
StreamWriter LWriter = File.CreateText(@"C:\Documents and
Settings\bassem\Desktop\data.txt");

for( int ColumnNo = 0 ; ColumnNo < ColumnsCount ; ColumnNo++)
{
//Here is the problem I can't get the value of the collumns in the
created array.
AllColumns[ColumnNo] = dataSet11.EmployeeSalary.Columns[ColumnNo][1];

}

Thanks.
 
Bassem bassem said:
This is the lines that give to you the information that you want and the
problem I have.

Well, it doesn't give me the information I asked for - namely the type
of EmployeeSalary. It looks like it's probably a DataTable though. Is
it?
//Here is the problem I can't get the value of the collumns in the
created array.
AllColumns[ColumnNo] = dataSet11.EmployeeSalary.Columns[ColumnNo][1];

The Columns property stores just the columns themselves - I suspect you
want the *rows*, and you fetch a specific column within that:

AllColumns[ColumnNo] = dataSet11.EmployeeSalary.Rows[1][ColumnNo];

That's assuming you want row number 1 (which will actually be the
*second* row).
 
Thanks Jon.

Yes the right property is the row.
and the right line of code is
AllColumns[ColumnNo] =
dataSet11.EmployeeSalary.Rows[0][ColumnNo].ToString();

Thanks a lot.
 
Back
Top