V
Vicky via DotNetMonster.com
Hi, I need help with "An object reference is required for the nonstatic
field, method, or property 'dataReader.Class1.data'"
Before I put folowing variable in class level, it works fine.
------------------
string[] data;
string valTicker = "";
string valPeriod = "";
---------------------
but after put them into class level I got above error msg.
I did to put
dataReader.Class1.valTicker = data[0];
it still not work.
PLease help with any info.
Thank you very much!
please see code blow.
-----------------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace dataReader
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
int FundID = 0;
string[] data;
string valTicker = "";
string valPeriod = "";
[STAThread]
public static void Main(string[] args)
{
StreamReader sr = new StreamReader("C:\\Vicky Development\\FundExplorer\\
Index\\Copy of IntlIndex\\TestA1DOWM.txt");
string currentLine = null;
string myConnString= "workstation id=HIRST-IT1;packet size=4096;"+
"Integrated Security=SSPI;initial catalog=FundManager42205;"+ "Data
Source=HIRST-IT1\\TEST;"+
"persist security info=True";
//read the text file
currentLine = sr.ReadLine();
currentLine = sr.ReadLine();
if(currentLine != null)
{
//string[] data = currentLine.Split(',');
data = currentLine.Split(',');
//string valTicker = data[0];
//valTicker = data[0];
valTicker = data[0];
//string valPeriod = data[1];
valPeriod = data[1];
string valDate = data[2];
string valNavCl = data[6];
Console.WriteLine(valTicker);
Console.WriteLine(valPeriod);
Console.WriteLine(valDate);
Console.WriteLine(valNavCl);
//string mySelectQuery = "SELECT FundID,Ticker, PeriodicityID FROM
Information3 WHERE (Ticker = '.A1DOW')AND (PeriodicityID = 4)";
string mySelectQuery = "SELECT FundID,Ticker, PeriodicityID FROM
Information3 WHERE Ticker = '" + valTicker + "'" + " AND PeriodicityID = 4";
SqlConnection cnn = new SqlConnection(myConnString);
SqlCommand cmd = new SqlCommand(mySelectQuery,cnn);
cnn.Open();
SqlDataReader myReader = null;
cmd.CommandText = mySelectQuery;
cmd.CommandType = CommandType.Text;
myReader= cmd.ExecuteReader();
for (int i=0; i < myReader.FieldCount; i++)
{
Console.Write("{0}", myReader.GetName(i).PadLeft(20, ' '));
}
Console.WriteLine("\n{0}", "".PadLeft(60, '-'));
//int FundID=0;
int PeriodicityID=0;;
string Ticker=null;
myReader.Read();
if (myReader.HasRows ==true)
{
int FundID = myReader.GetInt32(0);
Ticker = (string)myReader["Ticker"];
PeriodicityID = myReader.GetInt32(2);
Console.WriteLine("{0}{1}{2}",
FundID.ToString().PadLeft(20,' '),
Ticker.ToString().PadLeft(20, ' '),
PeriodicityID.ToString().PadLeft(20, ' '));
//***************************************************
cnn.Close();
myConnString= "workstation id=HIRST-IT1;packet size=4096;"+
"Integrated Security=SSPI;initial catalog=FundManager42205;"+ "Data
Source=HIRST-IT1\\TEST;"+
"persist security info=True";
cnn = new SqlConnection(myConnString);
cnn.Open();
// Start a local transaction.
SqlTransaction myTrans = cnn.BeginTransaction();
// Enlist the command in the current transaction.
SqlCommand myCommand = cnn.CreateCommand();
//SqlCommand myCommand = new SqlCommand(mySelectQuery,cnn);
myCommand.Transaction = myTrans;
try
{
mySelectQuery = "DELETE FROM Performance WHERE FundID =" + FundID ;
myCommand.CommandText = mySelectQuery;
myCommand.ExecuteNonQuery();
//insert
myCommand.CommandText = "Insert into Performance (FUNDID, FUNDDATE,[RETURN]
,NAV,FINAL,SOURCE,Periodicity)"+
"VALUES (20222, '" + valDate + "'" +",0.6012,'" + valNavCl + "'"
+",1,'Returs','month')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("records are deleted & updated in database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was deleted to database.");
}
finally
{
cnn.Close();
}
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
cnn.Close();
Console.ReadLine(); //to be sure your window will not close immediately
currentLine = sr.ReadLine();
} //end if hasrows
}//end of if(currentLine != null)
}
}
field, method, or property 'dataReader.Class1.data'"
Before I put folowing variable in class level, it works fine.
------------------
string[] data;
string valTicker = "";
string valPeriod = "";
---------------------
but after put them into class level I got above error msg.
I did to put
dataReader.Class1.valTicker = data[0];
it still not work.
PLease help with any info.
Thank you very much!
please see code blow.
-----------------------------------------------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace dataReader
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
int FundID = 0;
string[] data;
string valTicker = "";
string valPeriod = "";
[STAThread]
public static void Main(string[] args)
{
StreamReader sr = new StreamReader("C:\\Vicky Development\\FundExplorer\\
Index\\Copy of IntlIndex\\TestA1DOWM.txt");
string currentLine = null;
string myConnString= "workstation id=HIRST-IT1;packet size=4096;"+
"Integrated Security=SSPI;initial catalog=FundManager42205;"+ "Data
Source=HIRST-IT1\\TEST;"+
"persist security info=True";
//read the text file
currentLine = sr.ReadLine();
currentLine = sr.ReadLine();
if(currentLine != null)
{
//string[] data = currentLine.Split(',');
data = currentLine.Split(',');
//string valTicker = data[0];
//valTicker = data[0];
valTicker = data[0];
//string valPeriod = data[1];
valPeriod = data[1];
string valDate = data[2];
string valNavCl = data[6];
Console.WriteLine(valTicker);
Console.WriteLine(valPeriod);
Console.WriteLine(valDate);
Console.WriteLine(valNavCl);
//string mySelectQuery = "SELECT FundID,Ticker, PeriodicityID FROM
Information3 WHERE (Ticker = '.A1DOW')AND (PeriodicityID = 4)";
string mySelectQuery = "SELECT FundID,Ticker, PeriodicityID FROM
Information3 WHERE Ticker = '" + valTicker + "'" + " AND PeriodicityID = 4";
SqlConnection cnn = new SqlConnection(myConnString);
SqlCommand cmd = new SqlCommand(mySelectQuery,cnn);
cnn.Open();
SqlDataReader myReader = null;
cmd.CommandText = mySelectQuery;
cmd.CommandType = CommandType.Text;
myReader= cmd.ExecuteReader();
for (int i=0; i < myReader.FieldCount; i++)
{
Console.Write("{0}", myReader.GetName(i).PadLeft(20, ' '));
}
Console.WriteLine("\n{0}", "".PadLeft(60, '-'));
//int FundID=0;
int PeriodicityID=0;;
string Ticker=null;
myReader.Read();
if (myReader.HasRows ==true)
{
int FundID = myReader.GetInt32(0);
Ticker = (string)myReader["Ticker"];
PeriodicityID = myReader.GetInt32(2);
Console.WriteLine("{0}{1}{2}",
FundID.ToString().PadLeft(20,' '),
Ticker.ToString().PadLeft(20, ' '),
PeriodicityID.ToString().PadLeft(20, ' '));
//***************************************************
cnn.Close();
myConnString= "workstation id=HIRST-IT1;packet size=4096;"+
"Integrated Security=SSPI;initial catalog=FundManager42205;"+ "Data
Source=HIRST-IT1\\TEST;"+
"persist security info=True";
cnn = new SqlConnection(myConnString);
cnn.Open();
// Start a local transaction.
SqlTransaction myTrans = cnn.BeginTransaction();
// Enlist the command in the current transaction.
SqlCommand myCommand = cnn.CreateCommand();
//SqlCommand myCommand = new SqlCommand(mySelectQuery,cnn);
myCommand.Transaction = myTrans;
try
{
mySelectQuery = "DELETE FROM Performance WHERE FundID =" + FundID ;
myCommand.CommandText = mySelectQuery;
myCommand.ExecuteNonQuery();
//insert
myCommand.CommandText = "Insert into Performance (FUNDID, FUNDDATE,[RETURN]
,NAV,FINAL,SOURCE,Periodicity)"+
"VALUES (20222, '" + valDate + "'" +",0.6012,'" + valNavCl + "'"
+",1,'Returs','month')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Console.WriteLine("records are deleted & updated in database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
Console.WriteLine("Neither record was deleted to database.");
}
finally
{
cnn.Close();
}
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
cnn.Close();
Console.ReadLine(); //to be sure your window will not close immediately
currentLine = sr.ReadLine();
} //end if hasrows
}//end of if(currentLine != null)
}
}