When using the below code the error is:
Error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.
But when I use single ViewState it works properly.Either of ViewState for Gridview or for ListBox.But i want to use both.Please help to find my error.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["Filter"] = "ALL";
BindDropDown();
}
}
private void BindDropDown()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select parameters from IT_BOILER_PARAMETERS", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
}
private void BindGrid()
{
DataTable dt = new DataTable();
string date = "DATE,SHIFT,UNIT";
string parameter = date /*+ ',' + Label1.Text*/;
string query = "";
//Label2.Text = date + ',' + Label1.Text;
if (ViewState["Filter"].ToString().ToUpper() == "ALL")
{
query = "select " + parameter + " from IT_BOILER_DESK_1";
}
else
{
query = "select " + parameter + " from IT_BOILER_DESK_1 WHERE UNIT = @Filter";
}
SqlConnection con = new SqlConnection(strcon);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Filter", ViewState["Filter"].ToString());
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void CountryChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
ViewState["Filter"] = ddlCountry.SelectedValue;
this.BindGrid();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
Label lbl = new Label();
lbl.Text = "UNIT";
e.Row.Cells[2].Controls.Add(lbl);
DropDownList ddl = new DropDownList();
ddl.ID = "ddlCountry";
ddl.AutoPostBack = true;
ddl.Items.Add(new ListItem("All", "ALL"));
ddl.Items.Add(new ListItem("Unit I", "Unit I"));
ddl.Items.Add(new ListItem("Unit II", "Unit II"));
ddl.AppendDataBoundItems = true;
ddl.SelectedIndexChanged += new EventHandler(CountryChanged);
// this.BindCountryList(ddl);
e.Row.Cells[2].Controls.Add(ddl);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string projectnames = "";
bool firstValue = true;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true || ListBox1.Items.Count > 0)
{
if (!firstValue)
{
projectnames += ", " + ListBox1.Items[i].ToString();
}
else
{
projectnames += ListBox1.Items[i].ToString();
firstValue = false;
}
}
Label1.Text = projectnames;
}
BindGrid();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox1.Items.Clear();
ListBox3.Items.Clear();
ViewState.Clear();
Label1.Text = "";
SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from [IT_1_BOILER_DESK_1_PARAMETERS] where [pname]='" + this.DropDownList1.SelectedValue + "'", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
ListBox3.DataSource = ds.Tables[0];
ListBox3.DataTextField = "paramtext";
ListBox3.DataValueField = "param";
ListBox3.DataBind();
}
}
private string SelectedValue
{
get
{
return ViewState["SelectedValue"] != null ? (string)ViewState["SelectedValue"] : "";
}
set
{
ViewState["SelectedValue"] = value;
}
}
protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{
List<ListItem> arraylist1 = new List<ListItem>();
if (ListBox3.SelectedIndex >= 0)
{
for (int i = 0; i < ListBox3.Items.Count; i++)
{
if (ListBox3.Items[i].Selected)
{
if (!arraylist1.Contains(ListBox3.Items[i]))
{
arraylist1.Add(ListBox3.Items[i]);
if (!string.IsNullOrEmpty(SelectedValue))
{
SelectedValue += ",";
}
SelectedValue = SelectedValue + "'" + ListBox3.Items[i].Value + "'";
}
}
}
for (int i = 0; i < arraylist1.Count; i++)
{
if (!ListBox2.Items.Contains(((ListItem)arraylist1[i])))
{
ListBox2.Items.Add(((ListItem)arraylist1[i]));
}
ListBox3.Items.Remove(((ListItem)arraylist1[i]));
}
ListBox2.SelectedIndex = -1;
}
else
{
//
}
SqlConnection con = new SqlConnection(strcon);
SqlCommand command = new SqlCommand("select * from [IT_1_BOILER_DESK_1_PARAMETERS] where [param] IN (" + SelectedValue + ")", con);
SqlDataAdapter dataAadpter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
dataAadpter.Fill(ds);
if (ds != null)
{
ListBox1.DataSource = ds.Tables[0];
ListBox1.DataTextField = "param";
ListBox1.DataBind();
}
}
}