Out of Scope

G

Guest

The line below with nametest in it give out of scope when run. Why? There is
data in the table.

using System;
using System.Data;
using System.Data.OleDb;

namespace DataFM_Access_Data
{

class AccessConv
{

[STAThread]
static void Main()
{
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=mmrs.mdb";
string strAccessSelect = "Select * from Customers";
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add("Customers");
OleDbConnection myAccessConn = new OleDbConnection(strAccessConn);
myAccessConn.Open();
OleDbCommand myAccessDataSetCmd = new
OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataReader dtrAccess = myAccessDataSetCmd.ExecuteReader();
if (dtrAccess.Read())
{
string nameTest= dtrAccess["CompanyName"].ToString();
^gives out of scope?
}
myAccessConn.Close();
}

}
}
}
 
V

Vadym Stetsyak

Maybe it would be better to write
int custIdCol = dtrAccess.GetOrdinal("CompanyName");

while (dtrAccess.Read())
{
string nameTest= dtrAccess.GetString(custIdCol );
}
 
O

Octavio Hernandez

Dave,

Nothing seeems to be out of scope there. Maybe you try to access the
nameTest variable after closing the block:
if (dtrAccess.Read())
{
string nameTest= dtrAccess["CompanyName"].ToString();
}
Console.WriteLine(nameTest); // this would be 'out of scope'

Regards - Octavio

Dave said:
The line below with nametest in it give out of scope when run. Why? There
is
data in the table.

using System;
using System.Data;
using System.Data.OleDb;

namespace DataFM_Access_Data
{

class AccessConv
{

[STAThread]
static void Main()
{
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=mmrs.mdb";
string strAccessSelect = "Select * from Customers";
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add("Customers");
OleDbConnection myAccessConn = new OleDbConnection(strAccessConn);
myAccessConn.Open();
OleDbCommand myAccessDataSetCmd = new
OleDbCommand(strAccessSelect,myAccessConn);
OleDbDataReader dtrAccess = myAccessDataSetCmd.ExecuteReader();
if (dtrAccess.Read())
{
string nameTest= dtrAccess["CompanyName"].ToString();
^gives out of scope?
}
myAccessConn.Close();
}

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top