I am getting the following compiler erro while using the following code behind file in C#
Exception Details: System.NullReferenceExcept
ion: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MainMenu.Page_Load(Object sender, EventArgs e) +30
System.EventHandler.Invoke
(Object sender, EventArgs e) +0
System.Web.UI.Control.OnLo
ad(EventAr
gs e) +67
System.Web.UI.Control.Load
Recursive(
) +35
System.Web.UI.Page.Process
RequestMai
n() +731
<code>
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
;
using System.Web.Security;
public class MainMenu : System.Web.UI.Page {
protected Label lblTitle;
protected DataGrid dataGrid1;
protected bool Access;
public MainMenu() {
Page.Init += new System.EventHandler(Page_I
nit);
}
private void Page_Init(object sender, EventArgs e){
InitializeComponent();
}
private void InitializeComponent(){
this.Load += new System.EventHandler(this.P
age_Load);
}
private void Page_Load(object sender, System.EventArgs e){
string AccessLevel = Session["AccessLevel"].ToS
tring();
MainPageData(AccessLevel);
}
protected void MainPageData(string ALevel) {
Access = ALevel.Equals("admin");
if(Access){
lblTitle.Text = "Legislative Tracking Administrator.";
lblTitle.Visible = true;
string connectionString = "server=server; uid=username; " +
"pwd=password; database=database";
SqlConnection connection = new SqlConnection(connectionSt
ring);
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
string sql = "SELECT * FROM tblLaw WHERE Status <> 'C'";
command.CommandText = sql;
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = command;
dataAdapter.TableMappings.
Add("Table
", "tblLaw");
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGrid1.DataSource=dataS
et.Tables[
0];
dataGrid1.DataBind();
}
else{
lblTitle.Text = "Legislative Tracking.";
lblTitle.Visible = true;
}
}
}
</code>
Start Free Trial