J
John Holmes
I have a web interface where the user types in ID's one at a time. After an
ID is typed in, a button is clicked and the button click event has code that
does a query and returns a data reader and then appends the data to a
dataset that is built in the Page_Load code in the if(!isPostBack) block.
When I try to add a row in the button click event code I get an error saying
that "Object reference not set to an instance of an object". I'm saving the
dataset in a Session object and then restoring it in the else clause of the
if(!isPostBack) block. I've pasted code below, please help. I'm relatively
new at asp.net development but have lots of asp and ado experience.
Thanks,
John Holmes
--------------------------------- code ----------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ConnectionLibrary;
using System.Data.SqlClient;
namespace Affidavits
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlButton btnAddParcel;
public DataSet dsAssessorData;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
// create a dataset
BuildDataSet();
// save as session variable
Session["dsAssr"] = dsAssessorData;
}
else
{
DataSet dsAssessorData = new DataSet("Parcels");
dsAssessorData = (DataSet) Session["dsAssr"];
}
}
private void BuildDataSet()
{
DataTable dtParcels = new DataTable("tabParcels");
// add columns
DataColumn dcID = new DataColumn("ParcelID",typeof(string));
dcID.Unique = true;
dcID.AllowDBNull = false;
dtParcels.Columns.Add(dcID);
DataColumn dcAssessedValue = new DataColumn("Assessed", typeof(int));
dcAssessedValue.AllowDBNull = false;
dtParcels.Columns.Add(dcAssessedValue);
DataColumn dcAddress = new DataColumn("Address", typeof(string));
dcAddress.AllowDBNull = false;
dtParcels.Columns.Add(dcAddress);
DataSet dsAssessorData = new DataSet("Parcels");
dsAssessorData.Tables.Add(dtParcels);
// add a test row
DataRow rowNew = dsAssessorData.Tables[0].NewRow();
rowNew["ParcelID"] = "P34031";
rowNew["Assessed"] = "255000";
rowNew["Address"] = "88493" + " " + "Nowhere Road" +
", " + "Everywhere, WA 98834";
dsAssessorData.Tables[0].Rows.Add(rowNew);
DataGrid1.DataSource = dsAssessorData.Tables[0];
DataGrid1.DataBind();
}
#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.btnAddParcel.ServerClick += new
System.EventHandler(this.btnAddParcel_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//************* error occurs in this click event
private void btnAddParcel_ServerClick(object sender, System.EventArgs e)
{
// connect to database and retrieve data
string strCn =
SCConnection.GetConnectionString(SCConnection.Assessor,false);
string strSql = "select * from ExciseAffidavitView where [parcel number] =
'" +
TextBox2.Text + "'";
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
SqlCommand cmdGetAssrData = new SqlCommand(strSql,cn);
SqlDataReader drAssessor = cmdGetAssrData.ExecuteReader();
// I tried adding this code, but it didn't help
//DataSet dsAssessorData = new DataSet("Parcels");
//dsAssessorData = (DataSet) Session["dsAssr"];
while(drAssessor.Read())
{
//*************** error occurs on the following line
DataRow rowNew = dsAssessorData.Tables[0].NewRow();
rowNew["ParcelID"] = drAssessor.GetString(0);
rowNew["Assessed"] = drAssessor.GetString(15);
rowNew["Address"] = drAssessor.GetString(3) + " " + drAssessor.GetString(4)
+
", " + drAssessor.GetString(5);
dsAssessorData.Tables[0].Rows.Add(rowNew);
DataGrid1.DataSource = dsAssessorData.Tables[0];
DataGrid1.DataBind();
}
// update session variable
Session["dsAssr"] = dsAssessorData;
TextBox1.Text += TextBox2.Text;
TextBox2.Text = "";
cn.Close();
}
}
}
ID is typed in, a button is clicked and the button click event has code that
does a query and returns a data reader and then appends the data to a
dataset that is built in the Page_Load code in the if(!isPostBack) block.
When I try to add a row in the button click event code I get an error saying
that "Object reference not set to an instance of an object". I'm saving the
dataset in a Session object and then restoring it in the else clause of the
if(!isPostBack) block. I've pasted code below, please help. I'm relatively
new at asp.net development but have lots of asp and ado experience.
Thanks,
John Holmes
--------------------------------- code ----------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ConnectionLibrary;
using System.Data.SqlClient;
namespace Affidavits
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlButton btnAddParcel;
public DataSet dsAssessorData;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
// create a dataset
BuildDataSet();
// save as session variable
Session["dsAssr"] = dsAssessorData;
}
else
{
DataSet dsAssessorData = new DataSet("Parcels");
dsAssessorData = (DataSet) Session["dsAssr"];
}
}
private void BuildDataSet()
{
DataTable dtParcels = new DataTable("tabParcels");
// add columns
DataColumn dcID = new DataColumn("ParcelID",typeof(string));
dcID.Unique = true;
dcID.AllowDBNull = false;
dtParcels.Columns.Add(dcID);
DataColumn dcAssessedValue = new DataColumn("Assessed", typeof(int));
dcAssessedValue.AllowDBNull = false;
dtParcels.Columns.Add(dcAssessedValue);
DataColumn dcAddress = new DataColumn("Address", typeof(string));
dcAddress.AllowDBNull = false;
dtParcels.Columns.Add(dcAddress);
DataSet dsAssessorData = new DataSet("Parcels");
dsAssessorData.Tables.Add(dtParcels);
// add a test row
DataRow rowNew = dsAssessorData.Tables[0].NewRow();
rowNew["ParcelID"] = "P34031";
rowNew["Assessed"] = "255000";
rowNew["Address"] = "88493" + " " + "Nowhere Road" +
", " + "Everywhere, WA 98834";
dsAssessorData.Tables[0].Rows.Add(rowNew);
DataGrid1.DataSource = dsAssessorData.Tables[0];
DataGrid1.DataBind();
}
#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.btnAddParcel.ServerClick += new
System.EventHandler(this.btnAddParcel_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//************* error occurs in this click event
private void btnAddParcel_ServerClick(object sender, System.EventArgs e)
{
// connect to database and retrieve data
string strCn =
SCConnection.GetConnectionString(SCConnection.Assessor,false);
string strSql = "select * from ExciseAffidavitView where [parcel number] =
'" +
TextBox2.Text + "'";
SqlConnection cn = new SqlConnection(strCn);
cn.Open();
SqlCommand cmdGetAssrData = new SqlCommand(strSql,cn);
SqlDataReader drAssessor = cmdGetAssrData.ExecuteReader();
// I tried adding this code, but it didn't help
//DataSet dsAssessorData = new DataSet("Parcels");
//dsAssessorData = (DataSet) Session["dsAssr"];
while(drAssessor.Read())
{
//*************** error occurs on the following line
DataRow rowNew = dsAssessorData.Tables[0].NewRow();
rowNew["ParcelID"] = drAssessor.GetString(0);
rowNew["Assessed"] = drAssessor.GetString(15);
rowNew["Address"] = drAssessor.GetString(3) + " " + drAssessor.GetString(4)
+
", " + drAssessor.GetString(5);
dsAssessorData.Tables[0].Rows.Add(rowNew);
DataGrid1.DataSource = dsAssessorData.Tables[0];
DataGrid1.DataBind();
}
// update session variable
Session["dsAssr"] = dsAssessorData;
TextBox1.Text += TextBox2.Text;
TextBox2.Text = "";
cn.Close();
}
}
}