-1

I have connected my Visual studio 2013 to Oracle XE 11 Database. I'm trying to retrieve information from the tables I have in the database using the following code, but it seems that the array decor isn't getting initialised with the values I'm setting it with in the foreach loop. Please help.

conn.Open(); // it is an OleDbConnection

String[] decor;

DataTable table1 = new DataTable();

OleDbDataAdapter oda1 = new OleDbDataAdapter("select name from Product where Commodity_Type='decor'",conn);

oda1.Fill(table1);

int j=0;

foreach (DataRow row in table1.Rows)

            {

                decor[j] = row["name"].ToString();

                j++;

            }
Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41

2 Answers2

3

You did not initialise the variable decor

decor = new String[(table1.rows.Count())]

and that's the error you should have found by yourself.

Maheswaran Ravisankar
  • 17,652
  • 6
  • 47
  • 69
Slasko
  • 407
  • 2
  • 8
0

Try this code: You need to initialized decor after filling the table.

conn.Open();
DataTable table1 = new DataTable();
OleDbDataAdapter oda1 = new OleDbDataAdapter("select name from Product where Commodity_Type='decor'",conn);
oda1.Fill(table1);
String[] decor = new String[(table1.rows.Count())]
int j=0;
foreach (DataRow row in table1.Rows) {
     decor[j] = row["name"].ToString();
     j++;
}
Hazem Abdullah
  • 1,837
  • 4
  • 23
  • 41