R
Rodusa
I am getting this exception error which is driving me nuts.
System.NullReferenceException - Object reference not set to an instance
of an object
If I comment this line, I don't get any errors:
string Total_Dollar_Amount = GetTotal(sql,"test");
namespace SalesReport
{
/// <summary>
/// Summary description for SalesReport.
/// </summary>
public class SalesReport : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private ArrayList GetRowsArray()
{
ArrayList Ar = new ArrayList();
SqlConnection Conn = Common.GetConnection("sonny");
string sql = "SELECT * from Sn_SalesReport";
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
while (r.Read())
{
string sqlstatement = r["sqlstatement"].ToString();
Ar.Add(sqlstatement);
Response.Write(sqlstatement+"<br>");
}
r.Close();
Conn.Close();
return Ar;
}
private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList Ar = GetRowsArray();
// create the Thread array
Thread[] t = new Thread[ Ar.Count ];
int lIdx;
ProcessSQL sr;
for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
sr = new ProcessSQL(Ar[lIdx].ToString());
t[lIdx] = new Thread( new ThreadStart( sr.UpdateSalesReport ) );
t[lIdx].Start();
}
for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
// waiting for all the Threads to finish.
t[lIdx].Join();
}
}
}
public class ProcessSQL
{
private string sql;
public ProcessSQL (string sql)
{
this.sql = sql;
}
public void UpdateSalesReport()
{
try
{
string Total_Dollar_Amount = GetTotal(sql,"test");
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
}
public string GetTotal(string sql, string database)
{
sql = "Select reportname from SN_SalesReport";
string result = string.Empty;
try
{
SqlConnection Conn = Common.GetConnection(database);
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
if(r.Read())
{
// gets total
result = r.GetString(0);
}
r.Close();
if (Conn!=null)
{
Conn.Close();
}
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
return result;
}
}
}
System.NullReferenceException - Object reference not set to an instance
of an object
If I comment this line, I don't get any errors:
string Total_Dollar_Amount = GetTotal(sql,"test");
namespace SalesReport
{
/// <summary>
/// Summary description for SalesReport.
/// </summary>
public class SalesReport : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private ArrayList GetRowsArray()
{
ArrayList Ar = new ArrayList();
SqlConnection Conn = Common.GetConnection("sonny");
string sql = "SELECT * from Sn_SalesReport";
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
while (r.Read())
{
string sqlstatement = r["sqlstatement"].ToString();
Ar.Add(sqlstatement);
Response.Write(sqlstatement+"<br>");
}
r.Close();
Conn.Close();
return Ar;
}
private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList Ar = GetRowsArray();
// create the Thread array
Thread[] t = new Thread[ Ar.Count ];
int lIdx;
ProcessSQL sr;
for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
sr = new ProcessSQL(Ar[lIdx].ToString());
t[lIdx] = new Thread( new ThreadStart( sr.UpdateSalesReport ) );
t[lIdx].Start();
}
for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
// waiting for all the Threads to finish.
t[lIdx].Join();
}
}
}
public class ProcessSQL
{
private string sql;
public ProcessSQL (string sql)
{
this.sql = sql;
}
public void UpdateSalesReport()
{
try
{
string Total_Dollar_Amount = GetTotal(sql,"test");
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
}
public string GetTotal(string sql, string database)
{
sql = "Select reportname from SN_SalesReport";
string result = string.Empty;
try
{
SqlConnection Conn = Common.GetConnection(database);
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
if(r.Read())
{
// gets total
result = r.GetString(0);
}
r.Close();
if (Conn!=null)
{
Conn.Close();
}
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
return result;
}
}
}